![](/img/trans.png)
[英]DataGridView - Clear Paint on TopLeftHeaderCell datagridview
[英]datagridview clear and cellvalidate issue
我有一個包含多個列的dataGridView
。 第一列是標識符,必須是唯一的1或2位數字。 我使用單元驗證來確保標識符滿足這些條件。 用戶輸入所需數量的數據,然后導出數據。 導出數據后,程序通過調用grid.Clear()
和grid.Refresh()
清除dataGridView
。
在大多數情況下,此方法效果很好,但是有時在刪除行時,我收到一條錯誤消息,指示第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
原來,問題出在我有時會清除dataGridView,而該數據網格視圖的標識符列處於選中狀態,因此處於焦點位置。 通過關閉多重選擇並在調用clear()之前將焦點更改為其他單元格,我可以解決此問題。
在Grid_CellValidating事件中使用布爾值並繞過它,清除Datagridview后,您可以重置布爾值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.