When C # is used for development, the DataGridView display row numbers in HeaderCell,

Source: Internet
Author: User

When C # is used for development, the DataGridView display row numbers in HeaderCell,

When using C # To develop the WinForm program, the DataGridView is a table data control that we often use. When using this control, you often need to display the row number in the first column to know which row to operate.

At this time, we have two display methods:

One is directly displayed in HeaderCell.

The row number of the DataGridView is displayed on the HeaderCell.

The other is displayed in the data column.

The row number of the DataGridView instance is displayed on the data column.

The disadvantage of displaying data columns is obvious. Generally, the data displayed in the DataGridView has a dataset, and one column must be occupied in the dataset, adding a row number that is irrelevant to the original dataset to the dataset violates the principle of high cohesion and low coupling. More importantly, if a dataset needs to insert or delete a row, all affected row numbers must be modified and then rendered to the dview. this is inconvenient, especially when the data volume is large, the impact is even greater.

Therefore, the method shown in HeaderCell is used here.

Step 1

Select the DataGridView control, click the event button, find RowStateChanged, double-click the blank text box on the right, automatically bind the event and generate a method.

RowStateChanged event bound to the DataGridView

Write the following code in the method:

Private void maid (object sender, maid e)

{

// Displayed on HeaderCell

For (int I = 0; I <this. dataGridView1.Rows. Count; I ++)

{

DataGridViewRow r = this. dataGridView1.Rows [I];

R. HeaderCell. Value = string. Format ("{0}", I + 1 );

}

This. Maid ();

}

The HeaderCell data in the DataGridView is incomplete.

Step 2

The row number is not fully displayed. The solution is to set the RowHeadersWidthSizeMode attribute of the DataGridView to AutoSizeToAllHeaders, AutoSizeToDisplayedHeaders, or AutoSizeToFirstHeader.

The RowHeadersWidthSizeMode attribute of the DataGridView is set to AutoSizeToAllHeaders, AutoSizeToDisplayedHeaders, or AutoSizeToFirstHeader.

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.