【发布时间】:2016-02-02 20:57:41
【问题描述】:
所以,我想要实现的是从 Excel 中复制它并将其粘贴到非空白 DataGridView 视图中。我也想对不同的字符可读。我试图在粘贴后不要松开该行(第一张图片)。提前谢谢你。
First Image: http://postimg.org/image/u656ou22v/
错误图片:http://postimg.org/image/63o1evn77/
这是我在添加按钮中的代码:
DataObject o = (DataObject)Clipboard.GetDataObject();
if (o.GetDataPresent(DataFormats.Text))
{
if (dataGridView1.RowCount > 0)
dataGridView1.Rows.Clear();
if (dataGridView1.ColumnCount > 0)
dataGridView1.Columns.Clear();
bool columnsAdded = false;
string[] pastedRows = Regex.Split(o.GetData(DataFormats.Text).ToString().TrimEnd("\r\n".ToCharArray()), "\r\n");
int j = 0;
foreach (string pastedRow in pastedRows)
{
string[] pastedRowCells = pastedRow.Split(new char[] { '\t' });
if (!columnsAdded)
{
for (int i = 0; i < pastedRowCells.Length; i++)
dataGridView1.Columns.Add("col" + i, pastedRowCells[i]);
columnsAdded = true;
continue;
}
dataGridView1.Rows.Add();
int myRowIndex = dataGridView1.Rows.Count - 1;
using (DataGridViewRow myDataGridViewRow = dataGridView1.Rows[j])
{
for (int i = 0; i < pastedRowCells.Length; i++)
myDataGridViewRow.Cells[i].Value = pastedRowCells[i];
}
j++;
}
}
【问题讨论】:
标签: c# datagridview