ウィンドウフォームを設定しました。現在、SQL Server db の単一のテーブルにリンクされている単一の DataGridView があります。テーブル内の現在のデータを参照できます。
ユーザーが 1 列のデータを Excel シートから DGV にコピー アンド ペーストできるように設定するにはどうすればよいですか?
Excel で A1 に 'x' があり、A2 に 'y' がある場合、DGV に貼り付けたときに行数を保持する必要があります。つまり、この例では、まだ 2 行を超えています。
Code Projectから以下を適応させようとしました。行で失敗した場合(oCell.Value.ToString() != sCells[i])
、NullReferenceException was unhandled
何が間違っていますか?
private void uxChargeBackDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
PasteClipboard();
//uxChargeBackDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = Clipboard.GetText();
}
private void PasteClipboard()
{
try
{
string s = Clipboard.GetText();
string[] lines = s.Split('\n');
int iFail = 0, iRow = uxChargeBackDataGridView.CurrentCell.RowIndex;
int iCol = uxChargeBackDataGridView.CurrentCell.ColumnIndex;
DataGridViewCell oCell;
foreach (string line in lines)
{
if (iRow < uxChargeBackDataGridView.RowCount && line.Length > 0)
{
string[] sCells = line.Split('\t');
for (int i = 0; i < sCells.GetLength(0); ++i)
{
if (iCol + i < this.uxChargeBackDataGridView.ColumnCount)
{
oCell = uxChargeBackDataGridView[iCol + i, iRow];
if (!oCell.ReadOnly)
{
if (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;
}
}