【问题标题】:Datagridview in vb.net [duplicate]vb.net中的Datagridview [重复]
【发布时间】:2011-07-09 09:08:02
【问题描述】:

可能重复:
DataGridView - Validating for Cell

嗨...我有一个要求。用户在 textboxcolumn 中输入数据后,我不知道如何从数据网格中获取数据。我还想验证在 datagrid 单元格中输入的文本。

【问题讨论】:

标签: vb.net arrays dynamic datagridview keypress


【解决方案1】:

我希望这篇文章可以帮助您解决问题:

我构建了一个简单的程序,让用户可以将值放入 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

程序如何工作的示例:

希望能回答您的问题.. 下次请具体说明。 :D

【讨论】:

    【解决方案2】:

    这可能会回答你的问题:

    假设有:

    • 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
    

    【讨论】:

    • 在我对您的帖子做出回答并查看了您的问题后,我意识到您的问题是关于从 datagridview 获取值,而不是从单独的文本框中将值放入其中。如果我的理解是的,我会尝试找到另一个答案。顺便说一句,我不会删除我原来的答案,因为它可能会帮助其他读者的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多