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();
}
It is ridiculously slow. Pasting 500 rows with 5 columns takes about 30 seconds. Please take into consideration that there might be already data in the DataGridView and I don't necessarily want to override it (it all depends on the starting cell). So I don't know how could I use a DataTable and assign it as DataSource in case you had that idea.
You should always suspend the layout before updating the grid:
dgView.SuspendLayout();
// Do updates here
dgView.ResumeLayout();
I had my AutoSizeColumnsMode
set to AllCells
. I need to set it to None
when pasting. That makes a exponential difference. My 30 seconds now became less than half a second.
精彩评论