いくつかの場所にデータグリッドを持つ WinForms アプリケーションがあります。
データグリッドの 1 つで、右クリックのイベントを次のように設定しました。
private void dataBundles_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
try
{
if (e.Button == MouseButtons.Right)
{
var cell = dataBundles.HitTest(e.X, e.Y);
int cellRow = cell.RowIndex;
DataTable table = (DataTable)dataBundles.DataSource;
string bundleID = table.Rows[cellRow]["BundleID"].ToString();
dataBundles.Rows[cellRow].DefaultCellStyle.BackColor = Color.Red;
if (MessageBox.Show("Are you sure you want to delete Bundle ID '" + bundleID + "'?",
"Confirm Delete Bundle", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
int intBundleID = Int32.Parse(bundleID);
cBundle bundle = new cBundle(intBundleID);
bundle.Delete();
PopulateDocumentsTab();
}
else
{
dataBundles.Rows[cellRow].DefaultCellStyle.BackColor = Color.White;
}
}
}
catch (Exception eX)
{
string eM = "Error occurred when right clicking the Bundles datagrid";
aError err = new aError(eX, eM);
MessageBox.Show(eX.Message, eM);
}
}
ただし、何らかの理由で、このデータグリッドのどこを右クリックしても、rowindex は常に -1 を返しますが、これはもちろんグリッドから外れているため、エラーが発生します。
同じアプリケーション内の他のデータグリッドで右クリック イベントに同じコードを使用したため、何が間違っていたのかわかりません。それらはすべて正常に動作します。
私が見逃しているものは何でも単純なものになると確信していますが、私はこのコードにかなり長い間取り組んできました。
誰かが私を不幸から救ってくれますか?