locked
Display data in a textbox. Visual Basic Express Edition

    Question

  • Hi all,

    Maybe this is a easy question for you guys.

    I have a dgv with CustID column

    I will like to select a row and automatically see the data selected in a textbox.

    How can I do that?. Am really confused using the dgv properties in order to collect the data in the dgv.

    Please will appreciate to reply ASAP.

    Thanks for your help.

    Jorge
    Jorge Navarrete
    Wednesday, December 31, 2008 1:34 AM

Answers

  • Jorge,

      Assuming you have a datagridview named dgv, a column named CustID and a textbox named txtCustID, you could use the following in the cell click event of the datgridview(would work regardless of which cell was clicked in the dgv):

    txtCustID.Text = dgv.Rows(e.RowIndex).Cells("CustID").Value.ToString  
     

    If you want to only display the CustID in the textbox when they select the entire row, try using the RowHeaderMouseClick event of the datagridview.  You'll need to check for multiple selections and deal with it accordingly.

    I'm sure there are numerous other ways to do it, this is just one method.

    Jason
    Wednesday, December 31, 2008 2:09 AM
  • In the cell click event the e  is of type DataGridViewCellEventArgs, and has properties that contain the row and column indexes of the clicked cell.

    Example:

        Private Sub DataGridView1_CellClick(ByVal sender As System.ObjectByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick  
            Try 
                Dim thisCell As DataGridViewCell = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)  
                MessageBox.Show(thisCell.Value)  
            Catch ex As Exception  
                MessageBox.Show(ex.ToString, "Try to handle this exception")  
            End Try 
        End Sub 
    Wednesday, December 31, 2008 3:09 AM

All replies

  • Jorge,

      Assuming you have a datagridview named dgv, a column named CustID and a textbox named txtCustID, you could use the following in the cell click event of the datgridview(would work regardless of which cell was clicked in the dgv):

    txtCustID.Text = dgv.Rows(e.RowIndex).Cells("CustID").Value.ToString  
     

    If you want to only display the CustID in the textbox when they select the entire row, try using the RowHeaderMouseClick event of the datagridview.  You'll need to check for multiple selections and deal with it accordingly.

    I'm sure there are numerous other ways to do it, this is just one method.

    Jason
    Wednesday, December 31, 2008 2:09 AM
  • Hi Jason,

    Thxs for your help.

    Question. How should I initialize the variable rowindex?.
     
    am gettin the it does not belong to .....mouseEventArgs

    What is the e. for?

    Maybe it is implicit but am a beginner programmer.

    Thxs one more time
    Jorge Navarrete
    Wednesday, December 31, 2008 2:23 AM
  • Jason,

    I realized what you were saying......thxs for you help is working now.

    could you just tell me what does "e" represent?

    Many thanks

     


    Jorge Navarrete
    Wednesday, December 31, 2008 2:30 AM
  • In the cell click event the e  is of type DataGridViewCellEventArgs, and has properties that contain the row and column indexes of the clicked cell.

    Example:

        Private Sub DataGridView1_CellClick(ByVal sender As System.ObjectByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick  
            Try 
                Dim thisCell As DataGridViewCell = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)  
                MessageBox.Show(thisCell.Value)  
            Catch ex As Exception  
                MessageBox.Show(ex.ToString, "Try to handle this exception")  
            End Try 
        End Sub 
    Wednesday, December 31, 2008 3:09 AM
  • Hi All,

    Thxs for the help. It worked perfectly.

     

     


    Jorge Navarrete
    Wednesday, December 31, 2008 9:51 PM