C # setting blank columns of the datagridview

Source: Internet
Author: User

Hide blank columns:

dataGridView1.RowHeadersVisible = false;

The width of the blank column cannot be changed:

 this.dgv.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing;

To display the number of rows in a blank column:

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 = 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:

// Implement function datagridview dview add automatic number 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 by column for new rows added to the table. columnname = "auto number"; column. autoincrementseed = 1; column. autoincrementstep = 1; table. columns. add (column); table. merge (table); // merge merges the datatable this. datagridview1.datasource = table;

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.