2010-04-13 30 views
0

我在項目中使用帶有編輯功能的DataGrid。與手動編輯源數據相比,這很方便,但令人遺憾的是,這意味着我必須處理更多的驗證用戶輸入。驗證DataColumn的值

而我的問題基本上就是這樣。當我將DataGrid設置爲EDIT模式時,修改這些值並將其設置爲UPDATE,檢查我輸入的值是否與事實上與相應列的數據類型兼容的最佳方法是什麼?

即(簡單的例子)

// assuming 
DataTable dt = new DataTable(); 
dt.Columns.Add("aa",typeof(System.Int32)); 

DataGrid dg = new DataGrid(); 
dg.DataSource = dt; 
dg.DataBind(); 

dg.UpdateCommand += dg_Update; 

// this is the update handler 
protected void dg_Update(object src, DataGridCommandEventArgs e) 
{ 
    string newValue = (someValueIEnteredInTextBox); 

    // HOW DO I CHECK IF [newValue] IS COMPATIBLE WITH COLUMN "aa" ABOVE? 

    dt.LoadDataRow(newValue, true); 
} 

由於球員。任何線索都會有很多幫助。

回答

0

難道你不能使用比較驗證器嗎?它會檢查以下數據類型

String 
Double 
Date 
Currency 
Decimal 

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.comparevalidator.aspx

+0

CompareValidators實際上是這麼多HEL p,但TextBox()是由DataGrid的編輯過程自動生成的(即,通過使用EditCommandColumn)。 CompareValidators會好,我想,但每次DataGrid進入編輯模式時都必須校準它們。 絕對比普通的驗證更容易和更有效,但我想知道是否有更好的方法? – 2010-04-13 19:37:30