locked
DataGridView SelectedCell Value to textbox text

    Question

  • Just wanted  to ask a question on how to get a DataGridView SelectedCell Value into textbox text, and was hoping some one could explain it in simple terms i'm aware from previous attempts that:

     

    Code Snippet

    Private Sub DataGridView1_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick

    Dim CellSelection As String

    CellSelection = DataGridView1.SelectedCells.ToString

    TextBox1.AppendText(CellSelection)

    End Sub

     

    tells VB to place the collection refrence of the selected cell into TextBox1 when the user double clicks on the cell but i was hoping to append the value in the actual cell to the textbox. Thanks to  post http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2915736&SiteID=1 i've got the idea that i need to iterates through the collection the extract the cell's vales but the article that it's pointing to is slighly confusing, so if anyone is kind enought to tranlsate into terms that wold be clear in situation it would help

     

     

     

    Friday, February 29, 2008 9:52 PM

Answers

  • if you want just the value in a single selected cell then try this

     

    Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick

    MsgBox(Me.DataGridView1.CurrentCell.Value)

    End Sub

    Friday, February 29, 2008 10:38 PM

All replies

  • if you want just the value in a single selected cell then try this

     

    Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick

    MsgBox(Me.DataGridView1.CurrentCell.Value)

    End Sub

    Friday, February 29, 2008 10:38 PM
  • Hi Geoffrey,

    If you want to add text to textbox as you type in cell, try the code below. It does has some niggles (works fine for first cell entry, than there are some hiccups for consequtive entries) but you will get some idea.

     

    Private Sub DataGridView1_EditingControlShowing( _

          ByVal sender As Object, _

          ByVal e As System.Windows.Forms. _

          DataGridViewEditingControlShowingEventArgs) _

          Handles DataGridView1.EditingControlShowing

     

            '---restrict inputs on the fourth field---

            If Not e.Control Is Nothing Then

                Dim tb As TextBox = CType(e.Control, TextBox)

                '---add an event handler to the TextBox control---

                AddHandler tb.KeyPress, AddressOf TextBox_KeyPress

            End If

     

        End Sub

     

        Dim str As String = ""

     

        Private Sub TextBox_KeyPress( _

               ByVal sender As System.Object, _

               ByVal e As System.Windows.Forms.KeyPressEventArgs)

            TextBox2.Text = str

            If Not Char.IsControl(e.KeyChar) Then

                str &= e.KeyChar

                TextBox2.Text = str

            End If

        End Sub

     

    regards

    rajeev

    Saturday, March 01, 2008 4:53 AM