GridView Operation Summary1. Custom ColumnsCustomize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance Host Controls in Windows Forms DataGridView Cells inherit the DataGridViewTextBoxCell class to generate a new Cell class, then inherit the DataGridViewColumn to generate a new Column Class, and specify the CellTemplate as the new Cell class. The newly generated Column can be added to the DataGridView.
2. automatically adapt to column width
Programmatically Resize Cells to Fit Content in the Windows Forms DataGridView Control Samples: dview. autoSizeColumns (maid. headerAndDisplayedRows); DataGridView. autoSizeColumn (maid. headerOnly, 2, false); DataGridView. autoSizeRow (maid. columns, 2, false); DataGridView. autoSizeRows (maid. headerAndColumns, 0, dataGridView1.Rows. count, false );
3. objects can be bound and displayed.
Bind Objects to Windows Forms DataGridView Controls
4. You can change the line style of a table.
Change the Border and Gridline Styles in the Windows Forms DataGridView Control Samples: this. dataGridView1.GridColor = Color. bluevilet; this. dataGridView1.BorderStyle = BorderStyle. fixed3D; this. dataGridView1.CellBorderStyle = maid. none; this. dataGridView1.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle. single; this. dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle. single;
5. dynamically change whether the column is displayed, and dynamically change the Column Display sequence.
Change the Order of the Columns in the Windows Forms DataGridView Control Samples: mermersdatagri. columns ["CustomerID"]. visible = false; customersdatagri. columns ["ContactName"]. displayIndex = 0; customersdatagri. columns ["ContactTitle"]. displayIndex = 1; customersdatagri. columns ["City"]. displayIndex = 2; customersdatagri. columns ["Country"]. displayIndex = 3; customersdatagri. columns ["CompanyName"]. displayIndex = 4;
6. images can be displayed in columns.
Display Images in Cells of the Windows Forms DataGridView Control Samples: Icon treeIcon = new Icon (this. getType (), "tree. ico "); maid (); iconColumn. image = treeIcon. toBitmap (); iconColumn. name = "Tree"; iconColumn. headerText = "Nice tree"; dataGridView1.Columns. insert (2, iconColumn );
7. format the display content:
Format Data in the Windows Forms DataGridView Control Samples: this. dataGridView1.Columns ["UnitPrice"]. defaultCellStyle. format = "c"; this. dataGridView1.Columns ["ShipDate"]. defaultCellStyle. format = "d"; this. dataGridView1.DefaultCellStyle. nullValue = "no entry"; this. dataGridView1.DefaultCellStyle. wrapMode = maid. wrap; this. dataGridView1.Columns ["mermername"]. defaultCellStyle. alignment = maid. middleRight;
8. When dragging a column's scroll bar, you can freeze the specified column.
Freeze Columns in the Windows Forms DataGridView Control Samples: fix the specified column and the previous column. this. dataGridView1.Columns ["AddToCartButton"]. Frozen = true;
9. Obtain the selected cells, rows, and columns.
Get the Selected Cells, Rows, and Columns in the Windows Forms DataGridView Control Samples: see msdn.
10. Display Error information during Input
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 = maid. Commit) {MessageBox. Show ("CustomerID value must be unique .");}}
11. Use Virtual Mode for displaying large data volumesImplement Virtual Mode in the Windows Forms DataGridView Control
12. Set the specified column to read-only
Make Columns in the Windows Forms DataGridView Control Read-Only Samples: Maid ["CompanyName"]. ReadOnly = true;
13. Remove automatically generated Columns
Remove Autogenerated Columns from a Windows Forms DataGridView Control Sample: dataGridView1.AutoGenerateColumns = true; dataGridView1.DataSource = customerDataSet; dataGridView1.Columns. remove ("Fax"); or: dataGridView1.Columns ["mermerid"]. visible = false;
14. Custom selection mode
Set the Selection Mode of the Windows Forms DataGridView Control Sample: this. dataGridView1.SelectionMode = DataGridViewSelectionMode. FullRowSelect; this. dataGridView1.MultiSelect = false;
15. Set whether the cursor enters the cell editing mode (edit mode)Specify the Edit Mode for the Windows Forms DataGridView Control this. dataGridView1.EditMode = maid Mode. EditOnEnter;
16. Specify the default value for the new line.
Specify Default Values for New Rows in the Windows Forms DataGridView Control Sample: private void datagridview1_defavaluesneeded (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. Data Verification
Validate Data in the Windows Forms DataGridView Control Samples: private void maid cellvalidating (object sender, DataGridViewCellValidatingEventArgs e) {// Validate the CompanyName entry by disallowing empty strings. if (maid [e. columnIndex]. name = "CompanyName") {if (e. formattedValue. toString () = String. empty) {maid [e. rowIndex]. errorText = "Company Name must not be empty"; e. cancel = true ;}}}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service