2012-06-17 5 views
2

DataGridview 및 gridview 첫 번째 열에는 확인란이 포함되어 있고 확인란을 선택하고 싶습니다. 나에게 예외를주는 C#에서 응용 프로그램을 개발 중입니다. 객체 참조가 객체의 인스턴스로 설정되지 않았습니다. ' 코드는 다음과 같습니다gridview 열 확인 확인란

private void btnDelete_Click(object sender, EventArgs e) 
     { 
      StudentDAL s = new StudentDAL(); 

      try 
      { 
       for (int i = 0; i < this.dataGridView1.RowCount; i++) 
       { 

        if (!DBNull.Value.Equals(this.dataGridView1.Rows[i].Cells[0]) && (bool)this.dataGridView1.Rows[i].Cells[0].Value == true) 
        { 

         s.delete(Convert.ToInt32(this.dataGridView1.Rows[i].Cells[1].Value)); 
         i--; 

        } 

       } 
       this.dataGridView1.DataSource = s.getAll(); 

      } 
      catch (Exception nn) 
      { 


      } 


     } 

도와주세요.

+0

당신은'에 의해 먼저 체크 박스 제어를 얻어야한다 FindControl()'메서드를 호출 한 후 검사 여부를 확인하십시오. –

답변

0

먼저 당신은 당신이 좋아하는이 선택되어 확인하거나 할 수없는, 당신의 CheckBox 컨트롤을 찾을 수있다이 :

Int32 i; 
    CheckBox k; 

    for (i = 0; i < GridView1.Rows.Count; i++) 
     { 
      k = ((CheckBox)(GridView1.Rows[i].Cells[0].FindControl("chk"))); 
      if (k.Checked == true) 
      { 
       //your code here 
      } 
      else 
      { 
       //your code here 
      } 
     } 
0

초기화되지 않은 개체를 참조하려고합니다.이 인스턴스는 Row [i]라고 생각합니다.

for 루프 내에 중단 점을 넣고 (F10) 단계를 건너 뛰고 예외를 throw 할 때 어떤 위치에 있는지 확인하십시오.

+0

처음으로 예외를 throw하는 조건을 확인합니다. – Billz

+0

스택 추적을 게시 할 수 있습니까? –

0

이 더 추가 검증

foreach (DataGridViewRow rw in this.dataGridView1.Rows) 
{ 
    if (rw.Cells.Count > 2 && 
     rw.Cells[0].Value != DBNull.Value && String.IsNullOrWhiteSpace(rw.Cells[0].Value.ToString()) && 
     ((bool)rw.Cells[0].Value) && 
      rw.Cells[1].Value != DBNull.Value && String.IsNullOrWhiteSpace(rw.Cells[1].Value.ToString())) 
    { 
      s.delete(Convert.ToInt32(rw.Cells[1].Value)); 

    } 
}