Methods for displaying row numbers in a dview

Source: Internet
Author: User

Method 1:

The most common practice on the Internet is to use the RowPostPaint event of the DataGridView rowto draw a row number in RowHeaderCell:

 

RowPostPaint
 Private void maid (object sender, maid e)
{
Try
{
// Add a row number
SolidBrush v_SolidBrush = new SolidBrush (dataGridView1.RowHeadersDefaultCellStyle. ForeColor );
Int v_LineNo = 0;
V_LineNo = e. RowIndex + 1;

String v_Line = v_LineNo.ToString ();

E. Graphics. DrawString (v_Line, e. InheritedRowStyle. Font, v_SolidBrush, e. RowBounds. Location. X + 15, e. RowBounds. Location. Y + 5 );

}
Catch (Exception ex)
{
MessageBox. Show ("an error occurred when adding a row number. error Message:" + ex. Message, "operation failed ");
}
}

 

 

 

However, this method has poor performance when there is a large amount of data, and the RowPostPaint event is triggered every time the data is rolled.

 

 

Method 2:

My approach is to assign a value to the HeaderCell of each row.

I found someone mentioned this on the Internet, but I chose the above method because of the display problem. The specific problem is that when the row number exceeds two digits, such as 100 and 1000, the row indicator of the DataGridView▶It will squeeze the row number to the right, leading to incomplete reality. It will be displayed at 100.▶10.

In fact, there is still a problem with the RowsHeaderWidth size. You can enlarge the width of the column and display the row number!

I don't know if they have tried it. The method for drawing the row number above is also problematic when the row number is displayed.

 

Now that you know the problem

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.