日期:2014-05-18 浏览次数:20789 次
DataTable tbl = new DataTable(); //从数据库得到数据。 DataTable gridDataSource = new DataTable(); foreach(DataRow row in tbl.Rows) { DataColumn col = new DataColumn((string) row["yewu"]); gridDataSource.Columns.Add(col); } DataGridView grid = new DataGridView(); grid.DataSource = gridDataSource;
------解决方案--------------------
SqlCommand cmd = new SqlCommand("select yewu from table where id=1", cn);
SqlDataReader dr = cmd.ExecuteReader();
ArrayList list = new ArrayList();
while (dr.Read())
{
list.Add(dr.GetString(0));
}
dataGridView1.ColumnCount = list.Count;
for (int i = 0; i < list.Count;i++ )
{
dataGridView1.Columns[i].HeaderText = list[i].ToString();
}