1 #region 动态填充listvew和生产listview标题头
2 private void Resh(string sql)
3 {
4 SqlDataReader red = SQLHelper.getReader(sql);//读取
5 for (int i = 0; i < red.FieldCount; i++)//获取当前行中的列
6 {
7 ColumnHeader ch = new ColumnHeader();//创建标题头对象
8 ch.Width = 60;//宽60
9 ch.TextAlign = HorizontalAlignment.Center; //字体居中对齐
10 ch.Text = red.GetName(i);//获取列
11 this.lvView.Columns.Add(ch);//把所有列标题添加到listview
12 }
13 while (red.Read())
14 {
15 ListViewItem lvi = new ListViewItem();
16 lvi.SubItems[0].Text = red[0].ToString();
17 for (int i = 1; i < red.FieldCount; i++)//遍历其他几列
18 {
19 lvi.SubItems.Add(red[i].ToString());
20 }
21 this.lvView.Items.Add(lvi);
22 }
23 red.Close();
24 SQLHelper.Close();
25
26 }
27 #endregion
![](//images0.cnblogs.com/blog/497981/201305/27144044-e94a27737a044c36b3c32c60456dc153.x-png)