The DataGridView control displays the correct code and analysis for the line number

  • 2020-05-17 06:12:50
  • OfStack

A few days ago, I was writing a small program, using DataGridView, to dynamically display the line number. It's not so hard for GOOGLE to do 1, it doesn't matter for GOOGLE, there are a lot of problems. The following are basically some online solutions found by GOOGLE.


private void DataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) 
        { 

            for (int i = 0; i < e.RowCount; i++) 
            { 
                this.dgvKBRollUp.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; 
                this.dgvKBRollUp.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i + 1).ToString(); 
            } 
            for (int i = e.RowIndex + e.RowCount; i < this.dgvKBRollUp.Rows.Count; i++) 
            { 
                this.dgvKBRollUp.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; 
                this.dgvKBRollUp.Rows[i].HeaderCell.Value = (i + 1).ToString(); 
            }             
        } 

private void DataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e) 
        { 

            for (int i = 0; i < e.RowCount; i++) 
            { 
                this.dgvKBRollUp.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; 
                this.dgvKBRollUp.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i + 1).ToString(); 
            } 
            for (int i = e.RowIndex + e.RowCount; i < this.dgvKBRollUp.Rows.Count; i++) 
            { 
                this.dgvKBRollUp.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; 
                this.dgvKBRollUp.Rows[i].HeaderCell.Value = (i + 1).ToString(); 
            }             
        }

Anyone who has used this code should find that it is running erratically. The reason is that an RowsRemoved event throws an Index outof range exception. However, this is a piece of code with errors, almost full of the Internet, thousands of law of COPY, no one to correct.

Let's talk about the reason why this code is wrong:
In the RowsRemoved event, this event is also triggered when the DataGridView data is initially generated. At this point, the Rows.Count of the DataGridView control is 0. The following line of code is problematic:

this.dgvKBRollUp.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; 

e.RowIndex + i, which corresponds to Rows[0], but Rows.Count is still 0, Rows[0] does not exist. The Rows[0] control must have at least 1 line to exist. To avoid this error, just modify the code:

private void dgvKBRollUp_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e) 
        { 
            if (dgvKBRollUp.Rows.Count != 0) 
            { 
                for (int i = 0; i < e.RowCount; i++) 
                { 
                    this.dgvKBRollUp.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; 
                    this.dgvKBRollUp.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i + 1).ToString(); 
                } 

                for (int i = e.RowIndex + e.RowCount; i < this.dgvKBRollUp.Rows.Count; i++) 
                { 
                    this.dgvKBRollUp.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; 
                    this.dgvKBRollUp.Rows[i].HeaderCell.Value = (i + 1).ToString(); 
                } 

            }

This error can be avoided by simply adding a judgment on Rows.Count. I hope some friends of COPY on the Internet should also pay attention to it. When COPY comes to us in the future, we still need to verify 1 by ourselves. The spread of a false message is not good for beginners or for yourself.

Finally, I attach the code of e.RowIndex and e.RowCount in Microsoft MSDN:


System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder(); 
            messageBoxCS.AppendFormat("{0} = {1}", "RowIndex", e.RowIndex); 
            messageBoxCS.AppendLine(); 
            messageBoxCS.AppendFormat("{0} = {1}", "RowCount", e.RowCount); 
            messageBoxCS.AppendLine(); 
            MessageBox.Show(messageBoxCS.ToString(), "RowsRemoved Event"); 

With this code you can easily trace the values of e.RowIndex and e.RowCount in the event parameters. Of course you can DEBUG one. This is DEBUG. O ~ O (studying studying)


Related articles: