DataGridView 컨트롤은 행 번호의 올바른 코드를 표시합니다.
3995 단어 datagridview
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~
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
C#DataGridView의 데이터를 Excel로 내보내기텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.