DataGridView 컨트롤은 행 번호의 올바른 코드를 표시합니다.

3995 단어 datagridview
며칠 전에 DataGridView를 사용해서 동적 줄 번호를 보여 주려고 작은 프로그램을 썼습니다.힘들지 않게 GOOGLE을 했는데 GOOGLE은 괜찮습니다. 많은 문제점을 발견했습니다.다음은 기본적으로 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();

            }            

        }




 
이 코드를 사용한 사람만 있다면 이 코드가 잘못 실행된 것을 발견해야 한다.원인은 Rows Removed 이벤트에서 인덱스 outof range의 이상이 발생합니다.그러나 이렇게 잘못된 코드가 거의 인터넷 전체에 가득 차 있고 천편일률적인 COPY를 바로잡는 사람이 한 명도 없다.
먼저 이 코드가 잘못된 원인을 말해 봅시다.
RowsRemoved 이벤트에서 DataGridView 데이터를 처음 생성할 때도 이 이벤트를 터치합니다.이 때 DataGridView 컨트롤의 Rows.카운트는 0이야.다음 행 코드에 문제가 있습니다.
this.dgvKBRollUp.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;




e. RowIndex+i, 여기에 대응하는 것은Rows[0]이지만 Rows.카운트인지 0인지, Rows[0]는 존재하지 않습니다.Rows[0]가 존재하려면 DataGridView 컨트롤 행이 있어야 합니다.이 오류를 피하기 위해서 작은 수정 코드만 있으면 됩니다.
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();

                }

 

            }




한 쌍만 더하면 로스.Count의 판단은 이 오류를 피할 수 있다.인터넷의 일부 COPY 친구들도 주의해야 한다. 앞으로 COPY가 올 때 스스로 검증을 해야 한다.잘못된 정보를 함부로 퍼뜨리는 것은 일부 신출내기나 자신에게 그다지 좋지 않다.
마지막으로 Microsoft MSDN의 e.RowIndex 및 e.RowCount에 대한 코드를 첨부합니다.
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");




이 코드를 통해 이벤트 매개 변수의 e.RowIndex와 e.RowCount의 값을 쉽게 추적할 수 있습니다.물론 너는 DEBUG를 할 수 있다. 똑같다.내가 바로 DEBUG의 O() O~
 

좋은 웹페이지 즐겨찾기