将数据绑定到 DataGridView 控件非常简单和直观,在大多数情况下,只需设置 DataSource 属性即可。
在绑定到包含多个列表或表的数据源时,只需将 DataMember
属性设置为指定要绑定的列表或表的字符串即可。可以将许多类型的数据存储区用作数据源,DataGridView 控件支持标准
Windows 窗体数据绑定模型,也可以在没有绑定数据源的情况下操作 DataGridView
DataGridView 控件具有极高的可配置性和可扩展性。 当需要在 Windows 窗体应用程序中显示表格数据时,首先考虑使用
DataGridView 控件。 若要以小型网格显示只读值,或者若要使用户能够编辑具有数百万条记录的表,DataGridView
控件可以提供方便地进行编程以及有效地利用内存的解决方案。通过选择一些属性,可以轻松地自定义 DataGridView
控件的外观。
DataGridView
控件提供一种强大而灵活的以表格形式显示数据的方式。可以使用该控件显示小型到大型数据集的只读或可编辑视图。可以采用多种方法对
DataGridView 控件进行扩展,以将自定义行为内置在应用程序中。
例如,可以采用编程方式指定自己的排序算法,以及创建自己的单元格类型。DataGridView 控件可以显示数据存储区中的数据行。
支持多种类型的数据存储区。 数据存储区可以保存简单的非类型化数据(例如一维数组),也可以保存类型化数据(例如
DataSet)。
1、自定义列
Customize Cells and Columns in
the Windows Forms DataGridView Control by Extending Their Behavior
and Appearance
Controls in Windows Forms DataGridView Cells
DataGridViewTextBoxCell 类生成新的Cell类,然后再继承 DataGridViewColumn
生成新的Column类,并指定
CellTemplate为新的Cell类。新生成的Column便可以增加到DataGridView中去。
2、
列宽和行高自动调整的设定:
设定行高和列宽自动调整
//
设定包括Header和所有单元格的列宽自动调整
DataGridView1.AutoSizeColumnsMode =
DataGridViewAutoSizeColu
mnsMode.AllCells;
// 设定包括Header和所有单元格的行高自动调整
DataGridView1.AutoSizeRowsMode =
DataGridViewAutoSizeRows
Mode.AllCells;
AutoSizeColumnsMode 属性的设定值枚举请参照 msdn 的 DataGridViewAutoSizeRows
Mode
2)指定列或行自动调整
// 第一列自动调整
DataGridView1.Columns[0].AutoSizeMode =
DataGridViewAutoSizeColu
mnMode.DisplayedCells;
AutoSizeMode 设定为 NotSet
时,
默认继承的是
DataGridView.AutoSizeColumnsMode
3) 设定列头的高度和行头的宽度自动调整
// 设定列头的宽度可以自由调整
DataGridView1.ColumnHeadersHeightSizeM
ode =
DataGridViewColumnHeader
sHeightSizeMode.AutoSize;
// 设定行头的宽度可以自由调整
DataGridView1.RowHeadersWidthSizeMode =
DataGridViewRowHeadersWi
dthSizeMode.AutoSizeToAllHeaders;
4) 随时自动调整
a, 临时的,让列宽自动调整,这和指定AutoSizeColumnsMode属性一样。
// 让 DataGridView1 的所有列宽自动调整一下。
DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColu
mnsMode.AllCells);
// 让 DataGridView1 的第一列的列宽自动调整一下。
DataGridView1.AutoResizeColumn(0,
DataGridViewAutoSizeColu
mnMode.AllCells);
上 面调用的 AutoResizeColumns 和 AutoResizeColumn
当指定的是DataGridViewAutoSizeColu
mnMode.AllCells 的时候,
参数可以省略。即:DataGridView1.AutoResizeColumn(0) 和
DataGridView1.AutoResizeColumns()
b,临时的,让行高自动调整
// 让 DataGridView1 的所有行高自动调整一下。
DataGridView1.AutoResizeRows(DataGridViewAutoSizeRows
Mode.AllCells);
//让 DataGridView1 的第一行的行高自动调整一下。
DataGridView1.AutoResizeRow(0,
DataGridViewAutoSizeRowM
ode.AllCells);
上 面调用的 AutoResizeRows 和 AutoResizeRow
当指定的是DataGridViewAutoSizeRowM
ode.AllCells 的时候,
参数可以省略。即:DataGridView1.AutoResizeRow (0) 和
DataGridView1.AutoResizeRows()
c,临时的,让行头和列头自动调整
关于性能:通过 AutoSizeColumnsMode 或者 AutoSizeRowsMode
属性所指定的单元格进行自动调整时,如果调整次数过于多那么将可能导致性能下降,尤其是在行和列数比较多的情况下。在这时用
DisplayedCells 代替 AllCells 能减少非所见的单元格的调整,从而提高性能。
// 列头高度自动调整
DataGridView1.AutoResizeColumnHeadersH
eight();
// 行头宽度自动调整
DataGridView1.AutoResizeRowHeadersWidt
h(DataGridViewRowHeadersWi
dthSizeMode.AutoSizeToAllHeaders);
5)单元格自动适应窗体的宽度
Programmatically Resize Cells to Fit Content
in the Windows Forms DataGridView Control
DataGridView.AutoSizeColumns(DataGridViewAutoSizeColu
mnCriteria.HeaderAndDisplayedRows);
DataGridView.AutoSizeColumn(DataGridViewAutoSizeColu
mnCriteria.HeaderOnly,2,
false);
DataGridView.AutoSizeRow(DataGridViewAutoSizeRowC
riteria.Columns,2,
false);
DataGridView.AutoSizeRows(DataGridViewAutoSizeRowC
riteria.HeaderAndColumns,0,
dataGridView1.Rows.Count,
false);
3、可以绑定并显示对象
Bind Objects
to Windows Forms DataGridView Controls
4、
单元格的边框、
网格线样式的设定
1) DataGridView 的边框线样式的设定
DataGridView 的边框线的样式是通过 DataGridView.BorderStyle 属性来设定的。
BorderStyle 属性设定值是一个
BorderStyle 枚举: FixedSingle(单线,默认)、Fixed3D、None。
2) 单元格的边框线样式的设定
单元格的边框线的样式是通过 DataGridView.CellBorderStyle 属性来设定的。 CellBorderStyle
属性设定值是
DataGridViewCellBorderSt
yle 枚举。(详细参见 MSDN)
另外,通过 DataGridView.ColumnHeadersBorderStyle
和 RowHeadersBorderStyle
属性可以修改 DataGridView 的头部的单元格边框线样式。 属性设定值是
DataGridViewHeaderBorder
Style 枚举。(详细参见 MSDN)
3) 单元格的边框颜色的设定
单元格的边框线的颜色可以通过 DataGridView.GridColor 属性来设定的。默认是 ControlDarkDark
。但是只有在 CellBorderStyle 被设定为
Single、SingleHorizontal、SingleVertical
的条件下才能改变其边框线的颜色。同样,ColumnHeadersBorderStyle
以及
RowHeadersBorderStyle 只有在被设定为 Single 时,才能改变颜色。
4) 单元格的上下左右的边框线式样的单独设定
CellBorderStyle只能设定单元格全部边框线的式样。要单独改变单元格某一边边框式样的话,需要用到DataGridView.AdvancedCellBorderStyle属性。
同样,设定行头单元格的属性是: AdvancedRowHeadersBorder
Style,
设定列头单元格属性是:AdvancedColumnHeadersBor
derStyle。
5、动态改变列是否显示,和动态改变列的显示顺序
Change
the Order of the Columns in the Windows Forms DataGridView
Control
customersDataGridView.Columns["CustomerID"].Visible =
false;
customersDataGridView.Columns["ContactName"].DisplayIndex =
customersDataGridView.Columns["ContactTitle"].DisplayIndex =
customersDataGridView.Columns["City"].DisplayIndex = 2;
customersDataGridView.Columns["Country"].DisplayIndex =
customersDataGridView.Columns["CompanyName"].DisplayIndex =
设定 DataGridView 的 AllowUserToOrderColumns 为 True 的时候,
用户可以自由调整列的顺序。
当用户改变列的顺序的时候,其本身的 Index 不会改变,但是 DisplayIndex 改变了。也可以通过程序改变
DisplayIndex 来改变列的顺序。 列顺序发生改变时会引发 ColumnDisplayIndexChange
d
// DataGridView1的ColumnDisplayIndexChange
d事件处理方法
private void DataGridView1_ColumnDisplayIndexChange
d(object sender,
DataGridViewColumnEventA
rgs e)
Console.WriteLine("{0} 的位置改变到 {1} ",e.Column.Name,
e.Column.DisplayIndex);
6、在单元格中显示图像
Display
Images in Cells of the Windows Forms DataGridView Control
treeIcon = new Icon(this.GetType(), "tree.ico");
DataGridViewImageColumn iconColumn = new DataGridViewImageColumn
iconColumn.Image = treeIcon.ToBitmap();
iconColumn.Name = "Tree";
iconColumn.HeaderText = "Nice tree";
dataGridView1.Columns.Insert(2, iconColumn);
7、格式化显示内容:
Format Data
in the Windows Forms DataGridView Control
this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle.Format =
this.dataGridView1.Columns["ShipDate"].DefaultCellStyle.Format =
this.dataGridView1.DefaultCellStyle.NullValue = "no
entry";
this.dataGridView1.DefaultCellStyle.WrapMode =
DataGridViewWrapMode.Wrap;
this.dataGridView1.Columns["CustomerName"].DefaultCellStyle.Alignment
= DataGridViewContentAlign
ment.MiddleRight;
8、在拖动列的滚动条时可以将指定的列或行冻结
1)
DataGridViewColumn.Frozen 属性为 True 时, 该列左侧的所有列被固定,
横向滚动时固定列不随滚动条滚动而左右移动。这对于重要列固定显示很有用。
// DataGridView1的左侧2列固定
DataGridView1.Columns[1].Frozen = true;
但是,DataGridView.AllowUserToOrderColumns = True 时,固定列不能移动到非固定列,
反之亦然。
2) 行冻结
DataGridViewRow.Frozen 属性为 True 时, 该行上面的所有行被固定,
纵向滚动时固定行不随滚动条滚动而上下移动。
// DataGridView1 的上3行固定
DataGridView1.Rows[2].Frozen = true;
9、获取已选择的单元格,行,列的内容
Get the
Selected Cells, Rows, and Columns in the Windows Forms DataGridView
Control
1)获取选择的单元格:
private void selectedCellsButton_Click(object sender,
System.EventArgs e)
Int32
selectedCellCount =
dataGridView1.GetCellCount(DataGridViewElementState
s.Selected);
(selectedCellCount > 0)
if (dataGridView1.AreAllCellsSelected(true))
MessageBox.Show("All cells are selected", "Selected
Cells");
System.Text.StringBuilder sb = new
System.Text.StringBuilder();
for (int i = 0; i < selectedCellCount; i++)
sb.Append("Row: ");
sb.Append(dataGridView1.SelectedCells[i].RowIndex.ToString());
sb.Append(", Column: ");
sb.Append(dataGridView1.SelectedCells[i].ColumnIndex.ToString());
sb.Append(Environment.NewLine);
sb.Append("Total: " + selectedCellCount.ToString());
MessageBox.Show(sb.ToString(), "Selected Cells");
2)获取选择的行:
private void selectedRowsButton_Click(object sender,
System.EventArgs e)
Int32
selectedRowCount =
dataGridView1.Rows.GetRowCount(DataGridViewElementState
s.Selected);
(selectedRowCount > 0)
System.Text.StringBuilder sb = new