ags*_*mek 5 .net c# csv excel datagridview
我使用 DataGridView 控件来管理一个简单的字典(几列和几百行)。DataGridView 的功能几乎已经足够了。我可以添加新行、修改值并将数据从其中复制到 Excel。我无法做的一件事是将数据从 Excel 复制到我的控件。某些属性可以吗?或者需要一些代码才能做到这一点?
这是从 codeprojet 文章中编辑的。略有不同`
private void PasteClipboard()
{
try
{
string s = Clipboard.GetText();
string[] lines = s.Split('\n');
int iFail = 0, iRow = dgData.CurrentCell.RowIndex;
int iCol = dgData.CurrentCell.ColumnIndex;
DataGridViewCell oCell;
if (dgData.Rows.Count < lines.Length)
dgData.Rows.Add(lines.Length-1);
foreach (string line in lines)
{
if (iRow < dgData.RowCount && line.Length > 0)
{
string[] sCells = line.Split('\t');
for (int i = 0; i < sCells.GetLength(0); ++i)
{
if (iCol + i < this.dgData.ColumnCount)
{
oCell = dgData[iCol + i, iRow];
if (!oCell.ReadOnly)
{
if (oCell.Value==null|| oCell.Value.ToString() != sCells[i])
{
oCell.Value = Convert.ChangeType(sCells[i],
oCell.ValueType);
// oCell.Style.BackColor = Color.Tomato;
}
else
iFail++;
//only traps a fail if the data has changed
//and you are pasting into a read only cell
}
}
else
{ break; }
}
iRow++;
}
else
{ break; }
if (iFail > 0)
MessageBox.Show(string.Format("{0} updates failed due" +
" to read only column setting", iFail));
}
}
catch (FormatException)
{
MessageBox.Show("The data you pasted is in the wrong format for the cell");
return;
}
}
Run Code Online (Sandbox Code Playgroud)