dataGridView
複数の列を含むがあります。最初の列は識別子で、一意の 1 桁または 2 桁の数字である必要があります。セル検証を使用して、識別子がこれらの基準を満たしていることを確認します。ユーザーは必要な行数だけデータを入力し、データをエクスポートします。データをエクスポートした後、プログラムはdataGridView
と を呼び出しgrid.Clear()
て をクリアしますgrid.Refresh()
。
これはほとんどの場合うまく機能しますが、行が削除されているときに、列 1 のセル番号が一意ではないことを示すエラーが発生することがあります。このエラーを一貫して再現することはできません。通常、デバッガーに入ると、セル検証ステップでブレークしようとすると、期待どおりにクリア中に呼び出されていないことがわかります。ただし、クリア中に壊れることがあり、もちろん問題が発生します。
この問題の原因について何か考えがある人はいますか?
編集: これが私の cellValidate コードです:
private void Grid_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
string headerText = Grid.Columns[e.ColumnIndex].HeaderText;
switch (headerText)
{
case "Number":
if (!String.IsNullOrEmpty(e.FormattedValue.ToString()))
{
if (!Regex.IsMatch(e.FormattedValue.ToString(), @"(^\d{1,2}$)"))
{
MessageBox.Show("Identifier number must be a 1 or 2 digit positive number");
e.Cancel = true;
}
else
{
foreach (DataGridViewRow r in Grid.Rows)
{
if (r.Cells[0].Value != null)
{
if (e.FormattedValue.ToString() == r.Cells[0].Value.ToString())
{
//this message box pops up sometimes when I call Grid.Clear()
MessageBox.Show("Identifier number must unique");
e.Cancel = true;
}
}
}
}
}
break; //more cases for each column