DataGridViewセルに前の値を入力するための次のコードがあります。行0および列2以上の場合、左側のval、それ以外の場合は真上の値:
private void dataGridViewPlatypi_CellEnter(object sender, DataGridViewCellEventArgs args)
{
// TODO: Fails if it sees nothing in the previous cell
string prevVal = string.Empty;
if (args.RowIndex > 0)
{
prevVal = dataGridViewPlatypi.Rows[args.RowIndex - 1].Cells[args.ColumnIndex].Value.ToString();
} else if (args.ColumnIndex > 1)
{
prevVal = dataGridViewPlatypi.Rows[args.RowIndex].Cells[args.ColumnIndex-1].Value.ToString();
}
dataGridViewPlatypi.Rows[args.RowIndex].Cells[args.ColumnIndex].Value = prevVal;
}
これは、表示およびコピーする値がある限り、うまく機能します。ただし、セルが空白の場合は、次のようになります。
System.NullReferenceExceptionは
、オブジェクトのインスタンスに設定されていないユーザーコードMessage=Object参照によって処理されませんでした。
これはnull合体演算子を使用する機会だと思いますが(私の推測が適切であると仮定して)、どうすればそれを実装できますか?