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 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值