: #ffff66">DataGridVIEw.AutoSizeRows( t size="2"> customersDataGridVIEw.Columns["ContactName"].DisplayIndex = 0;
DataGridVIEwAutoSizeRowCriteria.HeaderAndColumns,
0, dataGridVIEw1.Rows.Count, false);
3、可以綁定並顯示對象
Bind Objects to Windows Forms DataGridVIEw Controls
4、可以改變表格線條風格
Change the Border and Gridline Styles in the Windows Forms DataGridVIEw Control
Samples:
this.dataGridVIEw1.GridColor = Color.BlueViolet;
this.dataGridVIEw1.BorderStyle = BorderStyle.Fixed3D;
this.dataGridVIEw1.CellBorderStyle = DataGridVIEwCellBorderStyle.None;
this.dataGridVIEw1.RowHeadersBorderStyle = DataGridVIEwHeaderBorderStyle.Single;
this.dataGridVIEw1.ColumnHeadersBorderStyle = DataGridVIEwHeaderBorderStyle.Single;
5、動態改變列是否顯示,和動態改變列的顯示順序
Change the Order of the Columns in the Windows Forms DataGridVIEw Control
Samples:
customersDataGridVIEw.Columns["CustomerID"].Visible = false;
customersDataGridVIEw.Columns["ContactTitle"].DisplayIndex = 1;
customersDataGridVIEw.Columns["City"].DisplayIndex = 2;
customersDataGridVIEw.Columns["Country"].DisplayIndex = 3;
customersDataGridVIEw.Columns["CompanyName"].DisplayIndex = 4;
6、可以在列中顯示圖像
Display Images in Cells of the Windows Forms DataGridVIEw Control
Samples:
Icon 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
Samples:
this.dataGridVIEw1.Columns["UnitPrice"].DefaultCellStyle.Format = "c";
this.fff66">dataGridVIEw1.Columns["ShipDate"].DefaultCellStyle.Format = "d";
this.dataGridVIEw1.DefaultCellStyle.NullValue = "no entry";
this.dataGridVIEw1.DefaultCellStyle.WrapMode = DataGridVIEwWrapMode.Wrap;
this.dataGridVIEw1.Columns["CustomerName"].DefaultCellStyle.Alignment =
DataGridVIEwContentAlignment.MiddleRight;
8、在拖動列的滾動條時可以將指定的列凍結
Freeze Columns in the Windows Forms DataGridVIEw Control
Samples:將指定列及以前的列固定不動
this.dataGridVIEw1.Columns["AddToCartButton"].Frozen = true;
9、獲取選擇的單元格,行,列
Get the Selected Cells, Rows, and Columns in the Windows Forms DataGridVIEw Control
Samples:
見msdn。
10、顯示錄入時出現的錯誤信息
Handle Errors that Occur During Data Entry in the Windows Forms DataGridVIEw Control
Samples:
private void dataGridVIEw1_DataError(object sender,
DataGridVIEwDataErrorEventArgs e)
{
// If the data source raises an exception when a cell value is
// commited, display an error message.
if (e.Exception != null &&
e.Context == DataGridVIEwDataErrorContext.Commit)
{
MessageBox.Show("CustomerID value must be unique.");
}
}
11、大數據量顯示采用Virtual Mode
Implement Virtual Mode in the Windows Forms DataGridVIEw Control
12、設置指定的列只讀
Make Columns in the Windows Forms DataGridVIEw Control Read-Only
Samples:
dataGridVIEw1.Columns["CompanyName"].ReadOnly = true;
13、移去自動生成的列
Remove Autogenerated Columns from a Windows Forms DataGridVIEw Control
Sample:
-COLOR: #ffff66">dataGridVIEw1.AutoGenerateColumns = true;
dataGridVIEw1.Columns.Remove ("Fax");
或:
dataGridVIEw1.Columns["CustomerID"].Visible = false;
14、自定義選擇模式
Set the Selection Mode of the Windows Forms DataGridVIEw Control
Sample:
this.dataGridVIEw1.SelectionMode = DataGridVIEwSelectionMode.FullRowSelect;
this.dataGridVIEw1.MultiSelect = false;
15、自定義設定光標進入單元格是否編輯模式(編輯模式)
Specify the Edit Mode for the Windows Forms DataGridVIEw Control
this.dataGridVIEw1.EditMode = DataGridVIEwEditMode.EditOnEnter;
16、新行指定默認值
Specify Default Values for New Rows in the Windows Forms DataGridVIEw Control
Sample:
private void dataGridVIEw1_DefaultValuesNeeded(object sender, System.Windows.Forms.DataGridVIEwRowEventArgs e)
{
e.Row.Cells["Region"].Value = "WA";
e.Row.Cells["City"].Value = "Redmond";
e.Row.Cells["PostalCode"].Value = "98052-6399";
e.Row.Cells["Region"].Value = "NA";
e.Row.Cells["Country"].Value = "USA";
e.Row.Cells["CustomerID"].Value = NewCustomerId();
}
17、數據驗證
Validate Data in the Windows Forms DataGridVIEw Control
Samples:
private void dataGridVIEw1_CellValidating(object sender,
DataGridVIEwCellValidatingEventArgs e)
{
// Validate the CompanyName entry by disallowing empty strings.
if (dataGridVIEw1.Columns[e.ColumnIndex].Name == "CompanyName")
{
if (e.FormattedValue.ToString() == String.Empty)
{
dataGridVIEw1.Rows[e.RowIndex].ErrorText =
"Company Name must not be empty";
e.Cancel = true;
}
}
}