以下代码是我每次想向 tableDocTypes DataGridView 添加行时调用的方法。调试显示 RowCount 正确递增并且传递的所有数据都是正确的,但是一旦表单显示它只显示最后一行。此外,显示的最后一行位于应该是空白的行中,因此我无法添加任何新行,因为星号旁边没有空白单元格。我的猜测是我不应该使用 tableDocTypes.RowCount - 1 作为索引,但它似乎正在正确递增,我找不到其他可以使用的东西。任何信息都有帮助。
private void addRowToDocTypeTable(bool docTypeValid, string formName, string formCabinet, string docType)
{
tableDocTypes.Rows.Add();
if (!docTypeValid)
{
// Change color to yellow and display IndexWarning icons
tableDocTypes.Rows[tableDocTypes.RowCount - 1].Cells[columnDocTypeImage.Index].Value = Properties.Resources.IndexWarning;
tableDocTypes.Rows[tableDocTypes.RowCount - 1].Cells[columnDocType.Index].Style.BackColor = Color.LightGoldenrodYellow;
}
else
{
// Index is good so display IndexCorrect and leave line white
tableDocTypes.Rows[tableDocTypes.RowCount - 1].Cells[columnDocTypeImage.Index].Value = Properties.Resources.IndexCorrect;
}
tableDocTypes.Rows[tableDocTypes.RowCount - 1].Cells[columnFormName.Index].Value = formName;
tableDocTypes.Rows[tableDocTypes.RowCount - 1].Cells[columnFormCabinet.Index].Value = formCabinet;
tableDocTypes.Rows[tableDocTypes.RowCount - 1].Cells[columnDocType.Index].Value = docType;
// Assign the rename menue to only the image column
tableDocTypes.Rows[tableDocTypes.RowCount - 1].Cells[columnDocTypeImage.Index].ContextMenuStrip = menuDocTypeEdit;
}