listview绑定数据
动态生成listview 可以自定义listview的样式 例如列头颜色 行渐变色等等 这里是像ListView添加数据 以读取数据存放到泛型为例子//初始化listview
int x = listView2.Location.X;
int y = listView2.Location.Y;
listView2.Clear();
listView2.Dispose();
listView2 = new ListView();
listView2.Refresh();
listView2.ForeColor = Color.Black;
listView2.View = View.Details;
listView2.FullRowSelect = true;
listView2.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.listView2.GridLines = true;
this.listView2.OwnerDraw = true;
this.listView2.Size = new System.Drawing.Size(681, 507);
this.listView2.Location = new System.Drawing.Point(x, y);
// 添加列名.
listView2.Columns.Add("列1", 72, HorizontalAlignment.Center);
listView2.Columns.Add("列2", 114, HorizontalAlignment.Center);
listView2.Columns.Add("列3", 135, HorizontalAlignment.Center);
try
{
/*假设读取的数据是存放到泛型list中*/
for (int i = 0; i < list.Count; i++)
{
ListViewItem listViewItem1 = new ListViewItem(new string[] { list[i].属性1.Trim(),list[i].属性2.Trim(), list[i].属性3.Trim() }, -1);
listView2.Items.AddRange(new ListViewItem[] { listViewItem1 });
}
}
catch
{
}
/*也可以考虑给listview2加其他事件*/
listView2.OwnerDraw = true;
listView2.DrawItem += new
DrawListViewItemEventHandler(listView2_DrawItem);
listView2.DrawSubItem += new
DrawListViewSubItemEventHandler(listView2_DrawSubItem);
listView2.DrawColumnHeader += new
DrawListViewColumnHeaderEventHandler(listView2_DrawColumnHeader);
listView2.MouseUp += new MouseEventHandler(listView2_MouseUp);
listView2.MouseMove += new MouseEventHandler(listView2_MouseMove);
listView2.ColumnWidthChanged += new ColumnWidthChangedEventHandler(listView2_ColumnWidthChanged);
listView2.Invalidated += new InvalidateEventHandler(listView2_Invalidated);