DataTable表格在LIstView中显示的方法

  /// <summary>
        /// 将DataTable表个数据显示在ListView中
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="lv"></param>
        public void ShowDataInListView(DataTable dt, ListView lv)
        {
            lv.Clear();
            lv.AllowColumnReorder = true;//用户可以调整列的位置
            lv.GridLines = true;//显示行与行之间的分隔线   
            lv.FullRowSelect = true;//要选择就是一行   
            lv.View = View.Details;//定义列表显示的方式  
            lv.Scrollable = true;//需要时候显示滚动条  
            lv.MultiSelect = false; // 不可以多行选择   
            lv.HeaderStyle = ColumnHeaderStyle.Clickable;
            lv.View = View.Details;
            int RowCount, ColumnCount;
            DataRow dr = null;
            if (dt == null) return;
            RowCount = dt.Rows.Count;
            ColumnCount = dt.Columns.Count;
            //添加列标题名
            for (int i = 0; i < ColumnCount; i++)
            {
                lv.Columns.Add(dt.Columns[i].Caption.Trim(),lv.Width/ColumnCount);
            }
            if (RowCount == 0) return;
            for (int i = 0; i < RowCount; i++)
            {
                dr = dt.Rows[i];
                lv.Items.Add(dr[0].ToString());
                for (int j = 1; j < ColumnCount; j++)
                {
                    lv.Items[i].SubItems.Add(dr[j].ToString());
                }
            }
       
        }

posted @ 2017-03-10 16:37  FranticPinK  阅读(3658)  评论(0编辑  收藏  举报