2011-03-05 90 views
0

可能重複:
DataGridView - Validating for Cell的DataGridView在vb.net

嗨...我有一個要求。用戶在文本框列中輸入數據後,我不知道如何從數據網格中獲取數據。我也想驗證datagrid單元格中輸入的文字。

+1

「如何驗證輸入到」DataGridView「單元格中的文本?」已經被問及答覆了無數次。你有更具體的問題嗎?你已經嘗試了什麼?爲什麼它不工作?請花點功夫先解決自己的問題。 – 2011-03-05 11:37:14

+0

可能重複的[關於datagridview控件的事件](http://stackoverflow.com/questions/5125596/about-datagridview-controls-event),[DataGridView - 爲細胞驗證](http://stackoverflow.com/questions/ 963869/datagridview-validating-for-cell),[Windows窗體 - 驗證DataGridView輸入](http://stackoverflow.com/questions/787360/windows-forms-validate-datagridview-input),[我如何驗證輸入到DataGridView中單元格的編輯控件?](http://stackoverflow.com/questions/2651105/how-can-i-validate-input-to-the-edit-control-of-a-cell-in- a-datagridview)等等。 – 2011-03-05 11:38:36

回答

0

這可能會回答你的問題:

假設有:

  • 2個文本框命名爲 「txt_X_Values」(得到列的 「x」 的值)和 「txt_Y_Values」(即獲取用戶輸入列Y);
  • 一個名爲「btnAdd_2_Table」的按鈕(單擊以將文本框中的數據添加到表中);
  • 一個名爲「dgv_RawData」的datagridview控件;和
  • 將包含/組件上面提到的控件形式(當然)..

用下面的代碼,當點擊該按鈕時,該按鈕將數據輸入存儲從文本框到其各自的陣列。在這裏,我已經聲明瞭一個名爲「Column_X」的數組,用於存儲txt_X_Values.text和Column_Y的txt_Y_Values.text值。 存儲值之後,我現在可以將它們顯示在datagridview單元格中(代碼如下所示,並帶有註釋「'將列/行添加到datagridview」)。有了這樣的過程,你可以添加過濾器/驗證器或任何你會稱之爲。你可以爲此聲明一個新的子或函數。

這裏是代碼:

Private Sub btnAdd_2_Table_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd_2_Table.Click 

    If Blank_Input(txt_X_Values.Text, txt_Y_Values.Text) Then 
     MessageBox.Show("You cannot have a blank data. Please provide numeric values to both columns.", _ 
         "ERROR ON INPUT", MessageBoxButtons.OK, MessageBoxIcon.Error) 
     If txt_X_Values.Text = "" Or txt_X_Values.Text = " " Then 
      txt_X_Values.Focus() 
     Else 
      txt_Y_Values.Focus() 
     End If 
    Else 
     Data_InputProc() 
     Display_Table_Proc() 

     ' add columns to datagridview 
     If rowCounter - 1 = 0 Then 
      dgv_RawData.Columns.Add("Column_X", x_Title) 
      dgv_RawData.Columns.Add("Column_Y", y_Title) 
     End If 

     ' add rows to datagridview 
     dgv_RawData.Rows.Add(x_Column(rowCounter - 1), y_Column(rowCounter - 1)) 

     ' enable reset 
     btnReset.Enabled = True 

     ' reset dot counters 
     dotCountX = 0 
     dotCountY = 0 
    End If 

    btnSave_Data.Enabled = True 

End Sub 

下面是我所做的功能代碼: *請注意,我只過濾/與這些代碼驗證數值。

(從文本框功能來存儲數據到陣列)

Public Sub Data_InputProc() 

    ' resize array 
    ReDim Preserve x_Column(total_Rows), y_Column(total_Rows) 

    ' engine 
    x_Column(rowCounter) = CDbl(txt_X_Values.Text) 
    y_Column(rowCounter) = CDbl(txt_Y_Values.Text) 
    '' next row 
    rowCounter += 1 
    total_Rows = rowCounter 

    ' ready value textbox for another input from user 
    txt_X_Values.Clear() 
    txt_Y_Values.Clear() 
    txt_X_Values.Focus() 

End Sub 

(函數/子來驗證數值...實際上此代碼只允許使用每數字輸入1個點的)

' keypress handler 
Public Sub NumericOnly(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles _ 
    txt_X_Values.KeyPress, txt_Y_Values.KeyPress 

    ' do not allow non-numeric characters but allow backspace and dot 
    If Not e.KeyChar Like "[0-9.]" And Asc(e.KeyChar) <> 8 Then 
     e.KeyChar = Nothing 
     ToolTip_Universal.Show("Only NUMERIC values are valid.", grpDataEntry, 300, 100, 1500) 
     ' do not allow multiple dots 
    ElseIf sender Is txt_X_Values And e.KeyChar Like "." Then 
     dotCountX += 1 
     If dotCountX > 1 And e.KeyChar Like "." Then _ 
      e.KeyChar = Nothing 
     ToolTip_Universal.Show("Only ONE DOT is allowed.", txt_X_Values, 130, 20, 1500) 
    ElseIf sender Is txt_Y_Values And e.KeyChar Like "." Then 
     dotCountY += 1 
     If dotCountY > 1 And e.KeyChar Like "." Then _ 
      e.KeyChar = Nothing 
     ToolTip_Universal.Show("Only ONE DOT is allowed.", txt_X_Values, 130, 20, 1500) 
    End If 

End Sub 
+0

當我對你的帖子提出了一個答案並且回顧了你的問題之後,我意識到你的問題是關於從datagridview獲取值,而不是從一個單獨的文本框中給它賦值。如果我的理解是對的,我會試着找到另一個答案。順便說一句,我不會刪除我原來的答案,因爲它可能會幫助其他讀者的問題。 – maohvlean19 2011-03-05 16:37:08

0

我希望這一個可以幫助你解決你的問題:

我構建了一個簡單的程序,讓用戶可以將值放到datagridview控件中。之後,按下按鈕以從該表中獲取值並將其存儲到二維數組中。數組可以隨意用來操縱數據。

代碼:

' declare array for storage of all values 
Dim array_of_all_values(,) As Object 
' number of columns and rows from the datagridview control 
Dim Num_of_Columns, Num_of_Rows As Integer 

' this block of code asks the user to how many columns does he want to add to the DGV 
Private Sub btnNo_of_Columns_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNo_of_Columns.Click 

    Num_of_Columns = txtCol_Num.Text 
    For columnCount = 1 To Num_of_Columns 
     dgv_Test.Columns.Add("Column_" & columnCount, InputBox("What is the header of Column " & columnCount & "?" & vbCrLf, "Column Header", "no header")) 
    Next 
    btnNo_of_Columns.Enabled = False 
    txtCol_Num.Clear() 
    dgv_Test.Focus() 

End Sub 

Private Sub btnGetSpecific_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetSpecific.Click 

    ' this code gets the specific value of the cell that is selected by the user (selection by mouse <left> click) 
    rtb_TestResult.Text = dgv_Test.Item(dgv_Test.CurrentCellAddress.X, dgv_Test.CurrentCellAddress.Y).Value 
    ' you may now insert the value of the cell into a variable 
    ' you may code for the specific validation that you require/desire 

End Sub 

Private Sub btnGetAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetAll.Click 
    Dim rowValue As String 
    ' this code will get values of all cells and record it to an array 
    Num_of_Rows = dgv_Test.RowCount - 1 
    ReDim Preserve array_of_all_values(Num_of_Rows, Num_of_Columns) 
    For dgvColumnIndex = 0 To Num_of_Columns - 1 
     For dgvRowIndex = 0 To Num_of_Rows - 1 

      array_of_all_values(dgvRowIndex, dgvColumnIndex) = dgv_Test.Item(dgvColumnIndex, dgvRowIndex).Value 
     Next 
    Next 
    ' you may now manipulate the inputs using the 2d array 
    ' you may now construct validation codes 

    ' this code displays the array values in table form 
    ' this is useful in checking arrays 
    For arr_X_index = 0 To UBound(array_of_all_values, 1) 
     For arr_Y_index = 0 To UBound(array_of_all_values, 2) 
      rowValue &= array_of_all_values(arr_X_index, arr_Y_index) & vbTab 
     Next 
     rtb_TestResult.Text &= rowValue & vbCrLf 
     rowValue = "" 
    Next 

End Sub 

的程序是如何工作的樣品: Sample of how the program worked

希望這能回答您的問題..請具體說明下一次。:D