2011-02-16 60 views

回答

1

如果您的網格數據主要是文本,則保留一個List<string>,該值保存所有插入到DataGridView中的值。

因此,每次在單元格中輸入數據時(您可以處理的事件,例如CellEndEditCellLeave),請在添加之前檢查它是否在字符串列表中。

如果是,那麼您發現網格中已包含一個值。

編輯 下面是一些示例代碼:

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
{ 
    string cellVal = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); 
    if (String.IsNullOrEmpty(listGridVals.Find(delegate(string s) { return s == cellVal; }))) 
     listGridVals.Add(cellVal); 
    else 
     MessageBox.Show("Value: " + cellVal + " already in the grid!"); 

} 

注意

這個程序非常基本的,它只是檢查是否在網格中被輸入值 已經在List<string>

但是,假設您已經在一個單元格中測試了測試並且已經在List<string>中。

當您刪除該值並輸入test1會發生什麼情況?那麼,測試仍然會在List<string>,下次你在另一個單元格中輸入測試你會得到MessageBox說已經有一個測試在網格中,這可能是錯誤的。

+0

好吧,我想編碼 – kumarsram 2011-02-16 05:16:26

相關問題