Csharp: listview control binding database from datatable
listView1.GridLines = true;//显示行与行之间的分隔线 listView1.FullRowSelect = true;//要选择就是一行 listView1.View = View.Details;//定义列表显示的方式 listView1.Scrollable = true;//需要时候显示滚动条 listView1.MultiSelect = true; // 可以多行选择 listView1.HeaderStyle = ColumnHeaderStyle.Clickable; if (dt.Columns.Count > 0) { //表頭名 foreach (DataColumn column in dt.Columns) { listView1.Columns.Add(column.ColumnName); } int r = 0; //每一行 foreach (DataRow row in dt.Rows) // Loop over the rows. { ListViewItem item = new ListViewItem(row[0].ToString()); for (int i = 1; i < dt.Columns.Count; i++) { if (i == 2)//因是二進制,顯示不了 { item.SubItems.Add(""); } else { item.SubItems.Add(row[i].ToString()); } } listView1.Items.Add(item); r++; } }
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace WindowsChineseCalender 11 { 12 /// <summary> 13 /// 20130117 14 /// 塗聚文 15 /// </summary> 16 public partial class TreeViewDataForm : Form 17 { 18 19 /// <summary> 20 /// 21 /// </summary> 22 /// <returns></returns> 23 public DataTable setData() 24 { 25 DataTable dt = new DataTable(); 26 dt.Columns.Add("CompanyUid", typeof(Guid)); 27 dt.Columns.Add("CompanyID", typeof(int)); 28 dt.Columns.Add("CompanyName", typeof(string)); 29 dt.Columns.Add("CompanyParentID", typeof(int)); 30 dt.Rows.Add(Guid.NewGuid(), 1, "塗聚文", 0); 31 dt.Rows.Add(Guid.NewGuid(), 2, "塗聚文1", 1); 32 dt.Rows.Add(Guid.NewGuid(), 3, "塗聚文2", 1); 33 dt.Rows.Add(Guid.NewGuid(), 4, "塗聚文3", 1); 34 dt.Rows.Add(Guid.NewGuid(), 5, "塗聚文31", 4); 35 dt.Rows.Add(Guid.NewGuid(), 6, "塗聚文32", 4); 36 dt.Rows.Add(Guid.NewGuid(), 7, "塗聚文321", 6); 37 38 39 return dt; 40 } 41 /// <summary> 42 /// 43 /// </summary> 44 public TreeViewDataForm() 45 { 46 InitializeComponent(); 47 } 48 /// <summary> 49 /// 50 /// </summary> 51 /// <param name="sender"></param> 52 /// <param name="e"></param> 53 private void TreeViewDataForm_Load(object sender, EventArgs e) 54 { 55 LoadData(); 56 } 57 /// <summary> 58 /// 59 /// </summary> 60 private void LoadData() 61 { 62 this.listView1.Items.Clear(); 63 listView1.GridLines = true;//显示行与行之间的分隔线 64 listView1.FullRowSelect = true;//要选择就是一行 65 listView1.View = View.Details;//定义列表显示的方式 66 listView1.Scrollable = true;//需要时候显示滚动条 67 listView1.MultiSelect = true; // 可以多行选择 68 listView1.HeaderStyle = ColumnHeaderStyle.Clickable; 69 70 DataTable dt = setData();// 71 //listView1.DataBindings = dt; 72 // listView1.Items.Add(new ListViewItem(new string[]{},0); 73 //表頭名 74 if (dt.Columns.Count > 0) 75 { 76 foreach (DataColumn column in dt.Columns) 77 { 78 listView1.Columns.Add(column.ColumnName); 79 } 80 } 81 int k = 0; 82 foreach (DataRow dr in dt.Rows) 83 { 84 //1種方式 85 //this.listView1.Items.Add(new ListViewItem(new string[] { dr["CompanyUid"].ToString(), dr["CompanyName"].ToString(), dr["CompanyParentID"].ToString(), dr["CompanyID"].ToString() },k++)); 86 87 //2種方式 88 ListViewItem lvitem = new ListViewItem(); 89 lvitem.SubItems[0].Text = dr["CompanyUid"].ToString(); 90 lvitem.SubItems.Add(dr["CompanyName"].ToString()); 91 lvitem.SubItems.Add(dr["CompanyParentID"].ToString()); 92 lvitem.SubItems.Add(dr["CompanyID"].ToString()); 93 listView1.Items.Add(lvitem); 94 } 95 } 96 /// <summary> 97 /// 98 /// </summary> 99 /// <param name="sender"></param> 100 /// <param name="e"></param> 101 private void listView1_DoubleClick(object sender, EventArgs e) 102 { 103 104 } 105 /// <summary> 106 /// 選擇的行 107 /// </summary> 108 /// <param name="sender"></param> 109 /// <param name="e"></param> 110 private void listView1_ItemActivate(object sender, EventArgs e) 111 { 112 int index = listView1.SelectedIndices[0]; 113 string k = ""; 114 if (listView1.SelectedIndices != null && listView1.SelectedIndices.Count > 0) 115 { 116 ListView.SelectedIndexCollection c = listView1.SelectedIndices; 117 k= listView1.Items[c[0]].Text; // 118 MessageBox.Show(k); 119 } 120 //選擇的行數 121 MessageBox.Show(listView1.SelectedIndices.Count.ToString()); 122 //共有多少列 123 MessageBox.Show(listView1.Items.Count.ToString()); 124 ListView.SelectedIndexCollection indexes = this.listView1.SelectedIndices; 125 foreach (int indext in indexes) 126 { 127 k = this.listView1.Items[indext].SubItems[0].Text; 128 k=k+","+this.listView1.Items[indext].SubItems[1].Text; 129 k = k + "," + this.listView1.Items[indext].SubItems[2].Text; 130 k = k + "," + this.listView1.Items[indext].SubItems[3].Text; 131 } 132 //顯示出一行的值 133 MessageBox.Show(k); 134 } 135 } 136 }
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)