string s = Clipboard.GetText().Replace("\r", " ");
string[] lines = s.Split('\n');
int row = dgView.CurrentCell.RowIndex;
int col = dgView.CurrentCell.ColumnIndex;
int linesCount = lines.Count();
if ((row + linesCount) - dgView.RowCount > 0) dgView.Rows.Add((row + linesCount) - dgView.RowCount);
asyncSqlResultsViewer.publicToolStripProgressBar.Maximum = linesCount;
asyncSqlResultsViewer.publicToolStripProgressBar.Step = 1;
asyncSqlResultsViewer.publicToolStripProgressBar.Visible = true;
dgView.ReadOnly = true;
foreach (string line in lines)
{
if (line.Length > 0)
{
string[] cells = line.Split('\t');
for (int i = 0; i < cells.GetLength(0); ++i)
{
if (col + i < dgView.ColumnCount)
{
dgView[col + i, row].Value = cells[i];
}
else
{
break;
}
}
row++;
}
else
{
break;
}
asyncSqlResultsViewer.publicToolStripProgressBar.PerformStep();
}
它太慢了.用5列粘贴500行大约需要30秒.请考虑到DataGridView中可能已经有数据,并且我不一定要覆盖它(所有数据都取决于起始单元格).因此,如果您有此想法,我不知道如何使用DataTable并将其分配为DataSource.
解决方法:
在更新网格之前,应始终暂停布局:
dgView.SuspendLayout();
// Do updates here
dgView.ResumeLayout();