C # Some common operations on the datagridview

Source: Internet
Author: User
C # Some common operations on the datagridview from the blog home page: @ Qiu

Dbfc # common techniques in datagridview
Only list the tips, which will be supplemented later
0 (the most basic technique). Get the content of a row (a cell) in a column
This. currentposition = This. datagridview1.bindingcontext
[This. Maid]. position;
Bookcontent = This. database. dataset. Tables [0]. Rows [This. currentposition] [21]. tostring (). Trim ();
MessageBox. Show (bookcontent );

1. Custom Columns

// Define the column width
This. Maid [0]. width = 80;
This. Maid [1]. width = 80;
This. datagridview1.columns [2]. width = 180;
This. dataserview1.columns [3]. width = 120;
This. Maid [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, then inherit the datagridviewcolumn to generate a new column Class, and specify
Celltemplate is a 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:
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. 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. Maid. None;
This. Maid. Single;
This. Maid = maid. 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:
Customersdatagri. 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 ");
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 Control
Samples:
This. Maid ["unitprice"]. defaultcellstyle. format = "C ";
This. Maid ["shipdate"]. defaultcellstyle. format = "D ";
This. Maid. nullvalue = "no entry ";
This. Maid. wrapmode = maid. Wrap;
This. Maid ["customername"]. defaultcellstyle. Alignment =
Datagridviewcontentalignment. middleright;

8. When dragging a column's scroll bar, you can freeze the specified column.
Freeze columns in the Windows Forms datagridview Control
Samples: fixed the specified column and the previous column
This. Maid ["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)
{
MessageBox. Show ("customerid value must be unique .");
}
}
11. Use virtual mode for displaying large data volumes
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 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. Maid = maid;
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 (Object sender,
Datagridviewcellvalidatingeventargs E)
{
// Validate the companyName entry by disallowing empty strings.
If (maid [E. columnindex]. Name = "companyName ")
{
If (E. formattedvalue. tostring () = string. Empty)
{
Datagridview1.rows [E. rowindex]. errortext =
"Company name must not be empty ";
E. Cancel = true;
}
}
}
18. data submitted to 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 (maid [I]. cells [J]. value );
}
DS. Tables [biaom. Trim ()]. Rows. Add (myrow );
}
}
Catch (exception)
{

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

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.