所以我有一個DataGridView用於顯示自定義模型列表。 這裏是一個模型的一些示例代碼:Databoud DataGridView每列驗證
public class TestModel
{
public IEnumerable<string> GetValidationErrors()
{
if (Value1 > 100 || Value1 <= 0)
yield return "Value1 can only be between 1 and 100 (inclusive)";
if (string.IsNullOrEmpty(Value2))
yield return "Value2 can not be empty";
}
public bool IsValid
{
get { return GetValidationErrors().Count() == 0; }
}
public int Value1
{
get; set;
}
public string Value2
{
get; set;
}
}
現在讓我們說我是綁定這些模型的列表(或IEnumerable的),像這樣
List<TestModel> list = Helpers.GetListOfTestModels();
dataGridView1.DataSource = list;
(我們可以認爲這個列表回報有效的,否則它不會被存儲)。
現在上的DataGridView中RowValidating
事件,我可以通過訪問IsValid
屬性驗證整個行並設置Rows[].ErrorText
,像這樣:
private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
var item = dataGridView1.Rows[e.RowIndex].DataBoundItem as TestModel;
if (item == null)
return;
if(!item.IsValid)
{
dataGridView1.Rows[e.RowIndex].ErrorText = "Failed Validation"; // Or use GetValidationErrors and concat them, but to be simple I've left that out
e.Cancel = true;
}
else
{
dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty;
}
}
現在,我真正想做的事情(不復制並粘貼遍佈整個地方的代碼),驗證每個屬性並設置Rows[].Cells[].ErrorText
屬性(這將顯示每個單元而不是整行的錯誤)。
我應該怎麼辦?
也許用自定義屬性的東西,然後反射獲取屬性名稱和訪問單元格的方式?
希望這一切都有道理!
+1你應該看看企業庫 - 驗證塊做類似這個... – 2010-07-27 21:20:08
臭味的是,這是一個一次性的應用程序,將在未來6個月內被取代!但我會看看,歡呼:) – PostMan 2010-07-27 21:24:20