In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "what are the common operations of DatagridView in C#", the content is simple and clear, and I hope it can help you solve your doubts. Let me lead you to study and learn this article "what are the common operations of DatagridView in C#".
0 (the most basic skill), get the contents of a row (a cell) in a column
This.currentposition = this.dataGridView1.BindingContext [this.dataGridView1.DataSource, this.dataGridView1.DataMember] .position; bookContent = this.database.dataSet.Tables [0] .rows [this.currentposition] [21] .ToString () .Trim (); MessageBox.Show (bookContent)
1. Custom columns
/ / define the column width this.dataGridView1.Columns [0] .Width = 80 × this.dataGridView1.Columns [1] .Width = 80th this.dataGridView1.Columns [2] .Width = 180mitthis.dataGridView1.Columns [3] .Width = 120transferthis.dataGridView1.Columns [4] .Width = 120th customize Cells and Columns in the Windows Forms DataGridView Control by Extending TheirBehavior and AppearanceHost Controls in Windows Forms DataGridView Cells
Inherit the DataGridViewTextBoxCell class to generate a new Cell class, and then inherit DataGridViewColumn to generate a new Column class, and specify
CellTemplate is 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 ControlSamples:DataGridView.AutoSizeColumns (DataGridViewAutoSizeColumnCriteria.HeaderAndDisplayedRows); DataGridView.AutoSizeColumn (DataGridViewAutoSizeColumnCriteria.HeaderOnly,2, false); DataGridView.AutoSizeRow (DataGridViewAutoSizeRowCriteria.Columns,2, false); DataGridView.AutoSizeRows (DataGridViewAutoSizeRowCriteria.HeaderAndColumns,0, dataGridView1.Rows.Count, false)
3. You can bind and display objects
Bind Objects to Windows Forms DataGridView Controls
4. The line style of the table can be changed.
Change the Border and Gridline Styles in the Windows Forms DataGridView ControlSamples: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. Dynamically change whether the columns are displayed, and dynamically change the display order of the columns
Change the Order of the Columns in the Windows Forms DataGridView ControlSamples:customersDataGridView.Columns ["CustomerID"]. Visible = false;customersDataGridView.Columns ["ContactName"]. DisplayIndex = 0x customersDataGridView.Columns ["ContactTitle"]. DisplayIndex = 1x customersDataGridView.Columns ["City"]. DisplayIndex = 2customersDataGridView.Columns ["Country"]. DisplayIndex = 3hand customersDataGridView.Columns ["CompanyName"]. DisplayIndex = 4
6. You can display an image in a column
Display Images in Cells of the Windows Forms DataGridView ControlSamples: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 the display content:
Format Data in the Windows Forms DataGridView ControlSamples:this.dataGridView1.Columns ["UnitPrice"] .DefaultCellStyle.Format = "c"; this.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. You can freeze the specified column when you drag the scroll bar of the column
Freeze Columns in the Windows Forms DataGridView ControlSamples: fix the specified column and previous columns this.dataGridView1.Columns ["AddToCartButton"] .Frozen = true
9. Get the selected cells, rows, and columns
Get the Selected Cells, Rows, and Columns in the Windows Forms DataGridView ControlSamples:
10. Display the error message that occurs during entry
Handle Errors that Occur During Data Entry in the Windows Forms DataGridView ControlSamples: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. Large amount of data is displayed by Virtual Mode.
Implement 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-OnlySamples:dataGridView1.Columns ["CompanyName"] .ReadOnly = true
13. Remove automatically generated columns
Remove Autogenerated Columns from a Windows Forms DataGridView ControlSample:dataGridView1.AutoGenerateColumns = true;dataGridView1.DataSource = customerDataSet;dataGridView1.Columns.Remove ("Fax"); or: dataGridView1.Columns ["CustomerID"] .Visible = false
14. Custom selection mode
Set the SelectionMode of the Windows Forms DataGridView ControlSample:this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;this.dataGridView1.MultiSelect = false
15. Customize whether the cursor enters the cell edit mode (edit mode)
Specify the EditMode for the Windows Forms DataGridView Controlthis.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
16. Specify default values for new lines
Specify DefaultValues for New Rows in the Windows Forms DataGridView ControlSample: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. Data verification
Validate Data in the Windows Forms DataGridView ControlSamples:private void dataGridView1_CellValidating (object sender,DataGridViewCellValidatingEventArgs e) {/ / Validate the CompanyName entry by disallowing empty strings.if (dataGridView1.ColumnIndex] .Name = = "CompanyName") {if (e.FormattedValue.ToString () = = String.Empty) {dataGridView1.Rows [e.RowIndex] .ErrorText = "CompanyName must not be empty"; e.Cancel = true;}
18. Submit the data to dataset
DataSet ds = new DataSet ("MyDataSet"); ds.Tables [biaom.Trim ()] .Rows.Clear (); try {for (int I = 0; I < dataGridView1.Rows.Count-1; iTunes +) {DataTable dt = ds.Tables [biaom.Trim ()]; DataRow myrow = ds.Tables [biaom.Trim ()] .NewRow (); for (int j = 0; j < dataGridView1.Columns.Count; jacks +) {myrow [j] = Convert.ToString (dataGridView1.Rows.Cells.Value) } ds.Tables [biaom.Trim ()] .Rows.Add (myrow);}} catch (Exception) {MessageBox.Show ("input type error!") ; return;} these are all the contents of this article entitled "what are the common operations of DatagridView in C#?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.