2013-04-30 31 views
0

我試圖驗證用戶在可編輯的gridview中輸入的內容。 我使用事件CellLeave驗證輸入,就像這樣:嘗試將gridview單元格值解析爲任何類型(驗證)

private void membersGrid_CellLeave(object sender, DataGridViewCellEventArgs e) 
{ 
    // Datatable that will hold the schema for the Members table 
    DataTable dtMeta; 

    // SqlDataAdapter is already filled and is now used to get the metadata 
    daAllMembers.FillSchema(dtMeta, SchemaType.Source); 

    // Define a type instance of the current column from the metadata table dtMeta 
    System.Type cellType = dtMeta.Columns[e.ColumnIndex].DataType; 

    // Get an object from the editted cell 
    Object objCellValue = membersGrid[e.ColumnIndex, e.RowIndex].Value; 

    // This is where i'm stuck, how can I do this? 
    cellType.TryParse(objCellValue); 
} 

我希望你明白我想要做的事。我基本上想要嘗試將該對象解析爲該表的元數據中定義的類型。

任何幫助表示讚賞;)

回答

0

我假設你只是想驗證文本輸入。其他單元格類型(如複選框或組合框)自然會限制用戶輸入。你可以做到以下幾點:

  1. 請從DataGridViewTextBoxCell派生一個抽象類:

    公共抽象類MyTextBox:DataGridViewTextBoxCell { 虛擬BOOL ValidateTextInput(); }

  2. 從這個基類中爲每個想要驗證的不同類型的數據網格視圖單元格類型派生一個類。例如:

    公共類StateCell:MyTextBox { 覆蓋ValidateTextInput(){ // 驗證該文本是一個有效的狀態縮寫 }}

  3. 將您的自定義,驗證數據網格視圖單元格類型到您的數據網格視圖(http://msdn.microsoft.com/en-us/library/aa730881(v=vs.80).aspx)。

+0

嘿謝謝,我會調查一下併發回給你! – DerpyNerd 2013-04-30 15:39:05