Common techniques in C#datagridview

Source: Internet
Author: User

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

Defining column widths
This.datagridview1.columns[0]. Width = 80;
THIS.DATAGRIDVIEW1.COLUMNS[1]. Width = 80;
THIS.DATAGRIDVIEW1.COLUMNS[2]. Width = 180;
THIS.DATAGRIDVIEW1.COLUMNS[3]. Width = 120;
THIS.DATAGRIDVIEW1.COLUMNS[4]. Width = 120;


Customize 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, and then inherit DataGridViewColumn to generate a new column class and specify
The celltemplate is the new cell class. The newly generated column can be added to the DataGridView.
2, automatically adapt to the width of the column
Programmatically Resize Cells to Fit Content in the Windows Forms DataGridView Control
Samples:
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, can change the style of the table line
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, dynamically change the column display, and dynamically change the column display order
The Order of the Columns in the Windows Forms DataGridView Control
Samples:
customersdatagridview.columns["CustomerID"]. Visible = false;
customersdatagridview.columns["ContactName"]. DisplayIndex = 0;
customersdatagridview.columns["ContactTitle"]. DisplayIndex = 1;
customersdatagridview.columns["City"]. DisplayIndex = 2;
customersdatagridview.columns["Country"]. DisplayIndex = 3;
customersdatagridview.columns["CompanyName"]. DisplayIndex = 4;
6. You can display the image in the column
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 the contents of the display:
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 = Datagridviewwrapmode.wrap;
this.datagridview1.columns["CustomerName"]. Defaultcellstyle.alignment =
Datagridviewcontentalignment.middleright;

8. You can freeze the specified column when you drag a column's scroll bar
Freeze Columns in the Windows Forms DataGridView Control
Samples: Fixed the specified column and the previous column
this.datagridview1.columns["Addtocartbutton"]. Frozen = true;
9. Get selected cells, rows, columns
Get the Selected Cells, Rows, and Columns in the Windows Forms DataGridView Control
Samples:
See MSDN.
10. Display error message when inputting
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, Big Data volume display using 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-only
Samples:
datagridview1.columns["CompanyName"]. ReadOnly = true;

13. Remove auto-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["CustomerID"]. 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, custom Set the cursor to enter the cell whether edit mode (edit mode)
Specify the Edit Mode for the Windows Forms DataGridView Control
This.dataGridView1.EditMode = Datagridvieweditmode.editonenter;
16. New line Specifies default value
Specify Default Values for New Rows in the Windows Forms DataGridView Control
Sample:
private void Datagridview1_defaultvaluesneeded (object sender, System.Windows.Forms.DataGridViewRowEv Entargs 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 validation
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 is not being empty";
E.cancel = true;
}
}
}
18. Data submitted to the dataset
DataSet ds = new DataSet ("myDataSet");
Ds. Tables[biaom. Trim ()]. Rows.clear ();
Try
{
for (int i = 0; i < datagridview1.rows.count-1; i++)
{
DataTable dt = ds. Tables[biaom. Trim ()];
DataRow myrow = ds. Tables[biaom. Trim ()]. NewRow ();
for (int j = 0; J < DataGridView1.Columns.Count; J + +)
{
MYROW[J] = convert.tostring (Datagridview1.rows[i]. CELLS[J]. Value);
}
Ds. Tables[biaom. Trim ()]. Rows.Add (Myrow);
}
}
catch (Exception)
{

MessageBox.Show ("input type Error! ");
Return
}

Common techniques in C#datagridview

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.