当我以电子方式修改 DataGridView 中的最后一行(新行,NewRowIndex)时,它不会像使用键盘输入数据时那样创建新行,或者如果我向 DataTable 添加新行,它将添加两行之一。任何想法如何处理这个?
int rowIndex = dataEntryGridView.SelectedCells[0].RowIndex;
GridModel gm = (GridModel)dataEntryGridView.DataSource;
if (gm == null) return;
int rowCount = gm.Rows.Count; // to many additions
//int rowCount = dataEntryGridView.Rows.Count; // no adition
if (dataEntryGridView.AllowUserToAddRows)
{
nrRows = rowsInClipboard.Length;
if (rowCount < (rowIndex + nrRows) && gm != null )
gm.AddRows(rowIndex + nrRows - rowCount);
}
int colIndex = dataEntryGridView.SelectedCells[0].ColumnIndex;
int colCount = dataEntryGridView.Columns.Count;
for (int iRow = 0; iRow < nrRows; iRow++)
{
string[] valuesInRow = rowsInClipboard[iRow].Split(columnSplitter);
for (int iCol = 0; iCol < valuesInRow.Length; iCol++)
{
if (colCount > colIndex + iCol)
{
DataGridViewCell cell = dataEntryGridView.Rows[rowIndex + iRow].Cells[colIndex + iCol];
if (!cell.ReadOnly)
{
gm.Rows[rowIndex + iRow][colIndex + iCol] = valuesInRow[iCol];
//cell.Value = valuesInRow[iCol];
}
}
}
}