2012-07-05 3 views
0

공개 함수에서 기존 DataGridview에 새 행을 추가하려고합니다. 내가 datagridview의 데이터 속성에 액세스하려고 할 때마다 null 값을 얻습니다. 데이터 소스 속성이 null이므로 새 요소를 추가 할 수 없습니다. 새 datagridviewrow를 만들었지 만 CreateCells 메서드는 열을 복사하지 않고 범위를 벗어난 인덱스를 throw합니다. 아무도 내가 뭘하려고하는지 잘못 알고 있을까?대리자 함수에서 DataGridview에 행을 추가 할 수 없습니다.

public void AddToGrid(Attendance newRecord) 
    { 
     try 
     { 
      DataGridViewRow newRow = new DataGridViewRow(); 
      newRow.CreateCells(AttendanceGrid); 
      newRow.Cells[1].Value = newRecord.EmployeeNumber; 
      newRow.Cells[2].Value = newRecord.ScheduledDate; 
      newRow.Cells[3].Value = newRecord.SIn; 
      newRow.Cells[4].Value = newRecord.SOut; 
      newRow.Cells[5].Value = newRecord.AIn; 
      newRow.Cells[6].Value = newRecord.AOut; 
      newRow.Cells[7].Value = newRecord.RecordCode; 
      newRow.Cells[8].Value = newRecord.ReasonCode; 
      newRow.Cells[9].Value = newRecord.Comments; 

      AttendanceGrid.Rows.Add(newRow); 
      AttendanceGrid.Refresh(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message + ": " + ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
    } 

사람이 내가 뭐하는 거지에 문제가 있음을 볼 수 있습니다 여기에

전체 기능에 대한 코드?

+0

DatagridView와 같은 Datatable과 같은 데이터 소스를 사용하고 있습니까? – Derek

답변

1

데이터 테이블과 같은 DatagridView에 대한 데이터 소스가 있어야합니다.

그러면 바인딩 소스를 통해 해당 DataTable을 Datagridview에 바인딩합니다.

DataTable에 새 행을 추가하면 DataGrid에 자동으로 반영됩니다.

관련 문제