开发者

Datagridview in vb.net [duplicate]

开发者 https://www.devze.com 2023-02-14 19:41 出处:网络
This question already has answers here: Closed 11 year开发者_如何学Pythons ago. Possible Duplicate:
This question already has answers here: Closed 11 year开发者_如何学Pythons ago.

Possible Duplicate:

DataGridView - Validating for Cell

hi...i have a requirement. i dont know how to get the data from the datagrid after the user entered data in textboxcolumn. i also want to validate the entered text in datagrid cell.


This may answer your question:

Suppose there are:

  • 2 textboxes named "txt_X_Values" (gets the values for column "x") and "txt_Y_Values" (that gets the user input for column Y);
  • a button named "btnAdd_2_Table" (click to add data from textboxes to table);
  • a datagridview control named "dgv_RawData"; and
  • (of course) a form that will contain the controls/components mentioned above..

With the following codes, when the button is clicked, the button will store the data input from the textboxes to their respective arrays. In here, i have declared an array named "Column_X" that stores values from txt_X_Values.text and Column_Y for txt_Y_Values.text. After the values are stored, i may now put/display them in the datagridview cells (codes are shown below..with the comment " ' add columns/rows to datagridview"). With such process, you can add filters/validators or whatever you would call it. You may declare a new sub or function for that.

Here are the codes:

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

Here are the codes for the functions that I have made: *Note that I am only filtering/validating numeric values with these codes.

(function to store data from textbox to arrays)

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

(function/sub to validate numeric values...actually this code only allows the use of 1 dot per numeric entry)

' 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


I hope this one helps you out with your problem:

I built a simple program that lets the user put values to the datagridview control. After that, a button is pressed to get the values from that table and store it to a 2D array. The array may then be used to manipulate data as you wish.

Code:

' 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:

Datagridview in vb.net [duplicate]

Hope that answers your problem.. please be specific next time. :D

0

精彩评论

暂无评论...
验证码 换一张
取 消