パブリック関数で既存の datagridview に新しい行を追加しようとしています。datagridview のデータ プロパティにアクセスしようとすると、null 値が返されます。datasource プロパティが null なので、新しい要素を追加できません。新しい datagridviewrow を作成しましたが、CreateCells メソッドは列をコピーせず、範囲外のインデックス エラーをスローします。私がここでやろうとしていることの何が問題なのか、誰かが知っていますか?
関数全体のコードは次のとおりです。
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);
}
}
私がしていることに問題がある人はいますか?