Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Events from gridview to textbox do not match the result in textbox in vb.net

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading