C # setting blank columns of the dataGridView

Source: Internet
Author: User
Tags drawtext

Hide blank columns:


[Csharp]
Maid = false;

Maid = false;
The width of the blank column cannot be changed:


[Csharp]
This. dgv. RowHeadersWidthSizeMode = System. Windows. Forms. Maid;

This. dgv. RowHeadersWidthSizeMode = System. Windows. Forms. Maid;


To display the number of rows in a blank column:


[Csharp]
Private void dgv_RowPostPaint (object sender, DataGridViewRowPostPaintEventArgs e)
{
// # Region method 1
// Using (SolidBrush B = new SolidBrush (dgv. RowHeadersDefaultCellStyle. ForeColor ))
//{
// Int linen = 0;
// Linen = e. RowIndex + 1;
// String line = linen. ToString ();
// E. Graphics. DrawString (line, e. InheritedRowStyle. Font, B, e. RowBounds. Location. X, e. RowBounds. Location. Y + 5 );
// SolidBrush B = new SolidBrush (Color. Red );
//}
// # Endregion
# Region method 2
Rectangle rectangle = new Rectangle (e. RowBounds. Location. X, e. RowBounds. Location. Y, dgv. RowHeadersWidth-4, e. RowBounds. Height );
TextRenderer. DrawText (
E. Graphics, (e. RowIndex + 1). ToString (),
Dgv. RowHeadersDefaultCellStyle. Font,
Rectangle,
Dgv. RowHeadersDefaultCellStyle. ForeColor,
TextFormatFlags. VerticalCenter | TextFormatFlags. HorizontalCenter
);
# Endregion
}

Private void dgv_RowPostPaint (object sender, DataGridViewRowPostPaintEventArgs e)
{
// # Region method 1
// Using (SolidBrush B = new SolidBrush (dgv. RowHeadersDefaultCellStyle. ForeColor ))
//{
// Int linen = 0;
// Linen = e. RowIndex + 1;
// String line = linen. ToString ();
// E. Graphics. DrawString (line, e. InheritedRowStyle. Font, B, e. RowBounds. Location. X, e. RowBounds. Location. Y + 5 );
// SolidBrush B = new SolidBrush (Color. Red );
//}
// # Endregion
# Region method 2
Rectangle rectangle = new Rectangle (e. RowBounds. Location. X, e. RowBounds. Location. Y, dgv. RowHeadersWidth-4, e. RowBounds. Height );
TextRenderer. DrawText (
E. Graphics, (e. RowIndex + 1). ToString (),
Dgv. RowHeadersDefaultCellStyle. Font,
Rectangle,
Dgv. RowHeadersDefaultCellStyle. ForeColor,
TextFormatFlags. VerticalCenter | TextFormatFlags. HorizontalCenter
);
# Endregion
}
If you are using the Table-bound Gridview, we recommend that you use the following method:

 

[Csharp]
// Implement the function to add an automatic number for the DataGridView
DataTable table = new DataTable ();
 
 
DataColumn column = new DataColumn ();
Column. AutoIncrement = true; // AutoIncrement gets or sets a value that indicates whether the column value is automatically incremented for new rows added to the table.
Column. ColumnName = "auto number ";
Column. AutoIncrementSeed = 1;
Column. AutoIncrementStep = 1;
Table. Columns. Add (column );
Table. Merge (table); // Merge merges the DataTable
This. Maid = table

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.