我正在尝试向公共函数中的现有 datagridview 添加新行。每当我尝试访问 datagridview 的任何数据属性时,我都会得到空值。数据源属性为空,因此我无法向其添加新元素。我创建了一个新的 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);
}
}
谁能看到我在做什么的问题?