有界DataGridView以编程方式修改最后一行(新行)

时间:2013-02-08 14:48:21

标签: c# datagridview

当我以编程方式修改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];
              }
           }
        }
     }

0 个答案:

没有答案