将excel数据粘贴到一个空白DataGridView - 索引超出范围异常 [英] Pasting excel data into a blank DataGridView - Index out of range exception
问题描述
所以,我想要实现的是将其从Excel复制并粘贴到一个空白的 DataGridView
view。
这是迄今为止的代码:
code> private void PasteClipboard(DataGridView myDataGridView)
{
DataObject o =(DataObject)Clipboard.GetDataObject();
if(o.GetDataPresent(DataFormats.Text))
{
string [] pastedRows = Regex.Split(o.GetData(DataFormats.Text).ToString()。TrimEnd(\\ \\\\.ToCharArray()),\r\\\
);
foreach(stringed pastedRow in pastedRows)
{
string [] pastedRowCells = pastedRow.Split(new char [] {'\t'});
使用(DataGridViewRow myDataGridViewRow = new DataGridViewRow())
{
for(int i = 0; i< pastedRowCells.Length; i ++)
myDataGridViewRow.Cells [i]。 Value = pastedRowCells [i];
myDataGridView.Rows.Add(myDataGridViewRow);
}
}
}
}
代码运行,我收到以下错误:
div>
在一些挖掘之后,我发现我必须首先添加列,然后添加一个新行,获取新创建的行的行索引,然后设置单元格值。
这是更新的代码:
DataObject o =(DataObject)Clipboard.GetDataObject();
if(o.GetDataPresent(DataFormats.Text))
{
if(myDataGridView.RowCount> 0)
myDataGridView.Rows.Clear();
if(myDataGridView.ColumnCount> 0)
myDataGridView.Columns.Clear();
bool columnsAdded = false;
string [] pastedRows = Regex.Split(o.GetData(DataFormats.Text).ToString()。TrimEnd(\r\\\
.ToCharArray()),\r\\\
);
foreach(stringed pastedRow in pastedRows)
{
string [] pastedRowCells = pastedRow.Split(new char [] {'\t'});
if(!columnsAdded)
{
for(int i = 0; i< pastedRowCells.Length; i ++)
myDataGridView.Columns.Add(col + i,pastedRowCells [i]);
columnsAdded = true;
继续;
}
myDataGridView.Rows.Add();
int myRowIndex = myDataGridView.Rows.Count - 1;
使用(DataGridViewRow myDataGridViewRow = myDataGridView.Rows [myRowIndex])
{
for(int i = 0; i< pastedRowCells.Length; i ++)
myDataGridViewRow .Cells [i] .Value = pastedRowCells [i];
}
}
}
}
这里它正在工作:
很高兴接受批评和有用的提示,以改善这一点。 此代码相当慢...
I have an excel sheet with the following:
So, what I am trying to achieve is copy this from Excel and paste it into a blank DataGridView
view.
This is the code I have so far:
private void PasteClipboard(DataGridView myDataGridView)
{
DataObject o = (DataObject)Clipboard.GetDataObject();
if (o.GetDataPresent(DataFormats.Text))
{
string[] pastedRows = Regex.Split(o.GetData(DataFormats.Text).ToString().TrimEnd("\r\n".ToCharArray()), "\r\n");
foreach (string pastedRow in pastedRows)
{
string[] pastedRowCells = pastedRow.Split(new char[] { '\t' });
using (DataGridViewRow myDataGridViewRow = new DataGridViewRow())
{
for (int i = 0; i < pastedRowCells.Length; i++)
myDataGridViewRow.Cells[i].Value = pastedRowCells[i];
myDataGridView.Rows.Add(myDataGridViewRow);
}
}
}
}
When the code runs, I am getting the following error:
Am I approaching this task at hand incorrectly?
After some digging around, I found that I have to add columns first, then add a new row, get the row index of the newly created row, and then set the cell values.
Here's the updated code:
DataObject o = (DataObject)Clipboard.GetDataObject();
if (o.GetDataPresent(DataFormats.Text))
{
if (myDataGridView.RowCount > 0)
myDataGridView.Rows.Clear();
if (myDataGridView.ColumnCount > 0)
myDataGridView.Columns.Clear();
bool columnsAdded = false;
string[] pastedRows = Regex.Split(o.GetData(DataFormats.Text).ToString().TrimEnd("\r\n".ToCharArray()), "\r\n");
foreach (string pastedRow in pastedRows)
{
string[] pastedRowCells = pastedRow.Split(new char[] { '\t' });
if (!columnsAdded)
{
for (int i = 0; i < pastedRowCells.Length; i++)
myDataGridView.Columns.Add("col" + i, pastedRowCells[i]);
columnsAdded = true;
continue;
}
myDataGridView.Rows.Add();
int myRowIndex = myDataGridView.Rows.Count - 1;
using (DataGridViewRow myDataGridViewRow = myDataGridView.Rows[myRowIndex])
{
for (int i = 0; i < pastedRowCells.Length; i++)
myDataGridViewRow.Cells[i].Value = pastedRowCells[i];
}
}
}
}
And here it is working:
Happy to accept criticisms and useful tips on improving this. This code is quite slow...
这篇关于将excel数据粘贴到一个空白DataGridView - 索引超出范围异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!