c# DataGridView绑定DataTable数据源并指定对应

//指定对应关系的关键在于两点:

1、dgv的 AutoGenerateColumns = false;

2、DataGridView新加入列的fieldNameColumn.DataPropertyName = "field-name";//这要和DataTable的属性名一样


  /// <summary>
         /// 设置GridView显示样式
         /// </summary>
         private  void  SetSettingGridViewDisplay()
         {
             //throw new NotImplementedException();
             this .GV_ColumnInfo.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
             this .GV_ColumnInfo.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Sunken;
             this .GV_ColumnInfo.CellBorderStyle = DataGridViewCellBorderStyle.Single;
             this .GV_ColumnInfo.RowHeadersVisible =  false ;
             this .GV_ColumnInfo.AutoGenerateColumns =  false ;
             this .GV_ColumnInfo.AllowUserToAddRows =  false ;
             this .GV_ColumnInfo.AllowUserToResizeRows =  false ;
         }


显示的时候指定一下:
如下:

C# code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  bindingSource.DataSource = itemAttributeDataTable;
                 this .GV_ColumnInfo.DataSource = bindingSource;
 
                 DataGridViewColumn fieldNameColumn =  new  DataGridViewTextBoxColumn();
                 fieldNameColumn.HeaderText =  "field_name" ;
                 fieldNameColumn.DataPropertyName =  "field-name" ; //这个很重要,对应的是你数据源的字段名
                 this .GV_ColumnInfo.Columns.Add(fieldNameColumn);
 
                 DataGridViewColumn idColumn =  new  DataGridViewTextBoxColumn();
                 idColumn.HeaderText =  "id" ;
                 idColumn.DataPropertyName =  "id" ;
                 this .GV_ColumnInfo.Columns.Add(idColumn);
 
                 DataGridViewColumn identifierColumn =  new  DataGridViewTextBoxColumn();
                 identifierColumn.HeaderText =  "identifier" ;
                 identifierColumn.DataPropertyName =  "identifier" ;
                 this .GV_ColumnInfo.Columns.Add(identifierColumn);
 
                 //下拉列表绑定时候,DataPropertyName和ValueMember做对比,
                 DataGridViewComboBoxColumn typeColumn =  new  DataGridViewComboBoxColumn();
                 typeColumn.DataSource = GetComBoxDataSource();
                 typeColumn.Width = 200;
                 typeColumn.DataPropertyName =  "type" ;
                 typeColumn.DisplayMember =  "Text" ;
                 typeColumn.ValueMember =  "Values" ;
                 typeColumn.HeaderText =  "type" ;
                 //typeColumn = ComboBoxStyle.DropDownList;
                 this .GV_ColumnInfo.Columns.Add(typeColumn);
 
                 DataGridViewColumn lengthColumn =  new  DataGridViewTextBoxColumn();
                 lengthColumn.HeaderText =  "length" ;
                 lengthColumn.DataPropertyName =  "length" ;
                 this .GV_ColumnInfo.Columns.Add(lengthColumn);


楼主这个问题有几种方法可以解决,我所知道的就有三种,
1,在源头过滤,即使用SQL语句时就过滤掉。
2,把DATAGRID的字段的visble设为false.
3,指定DATAGRID的列所对应的DATATABLE的列。
可能还有其它方法,楼主还是多试试吧。

你可能感兴趣的:(c# DataGridView绑定DataTable数据源并指定对应)