Home > Software design >  Events from gridview to textbox do not match the result in textbox in vb.net
Events from gridview to textbox do not match the result in textbox in vb.net

Time:04-05

should the textbox date (txt.DTE) be empty instead of containing the date from the previous date that I did double click cell gridview. Is there a solution or is my code wrong?.

Note : I use visual studio 2010

 Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        x = DataGridView1.Rows.IndexOf(DataGridView1.CurrentRow)
        txtCODE.Text = DataGridView1.Rows(x).Cells(0).Value.ToString()
        Dim dte = DataGridView1.Rows(x).Cells(1).Value.ToString()
        If dte <> Nothing Then
            txtDTE.Text = CDate(dte)
        End If
        txtQTY.Text = DataGridView1.Rows(x).Cells(2).Value.ToString()
        txtPRICE.Text = DataGridView1.Rows(x).Cells(3).Value.ToString()
    End Sub

view gridview

CodePudding user response:

Add an else code in your if clause to clear the value of the txtDTE if the date column is blank.

Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        x = DataGridView1.Rows.IndexOf(DataGridView1.CurrentRow)
        txtCODE.Text = DataGridView1.Rows(x).Cells(0).Value.ToString()
        Dim dte = DataGridView1.Rows(x).Cells(1).Value.ToString()
        If dte <> Nothing Then
            txtDTE.Text = CDate(dte)
        else
            ' CLEAR txtDTE if dt = nothing
            txtDTE.clear()
        End If
        txtQTY.Text = DataGridView1.Rows(x).Cells(2).Value.ToString()
        txtPRICE.Text = DataGridView1.Rows(x).Cells(3).Value.ToString()
    End Sub

Another advice:
You can use e.RowIndex instead of DataGridView1.Rows.IndexOf(DataGridView1.CurrentRow) which also returns the current row index.

Example:

x = e.RowIndex()

Instead of

x = DataGridView1.Rows.IndexOf(DataGridView1.CurrentRow)
  • Related