none
DATAGRID SELECTED ROW

    Question

  • Hi everybody, can anyone tell me how can I get the selected row from a datagrid?

    I try this code and get an error:

     

     

    Dim d As DataRow

    d = dtgLookUp.SelectedRows.Item(I)

    I also tried :

    dtgLookUp.CurrentRow.Clone

    but none is good I keep getting the message:

    value of type system.windows.forms.datagrid can not be converted to system.data.datarow 

    Please Help


    hagita
    Sunday, November 14, 2010 8:18 AM

All replies

  • Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    Dim i, j As Integer
    i=e.rowIndex
    j=e.columnIndex
    TextBox1.Text = DataGridView1.Item(i, j).Value
    End Sub

     

    Try this....


    Attributor 2.0
    Monday, November 15, 2010 4:44 AM
  • are you usuing data binding???

    Suppose you have a binding source, boundItems. and an DataGridView itemgridView where the datasource is boundItems.

    and finally, boundItems is set to a collection of a class myItem.

    then to get the current item selected iun the grid, it is simply

    CType(boundItems.Current, myItem)


    gimme some slamming techno!!!!
    Monday, November 15, 2010 5:03 AM
    Moderator
  • I see all kind of answers for a datagridview, can you tell what kind of datagrid you are using

    WindowsForms, ASP.Net, WPF/Silverligth or even a 3th party.

    I hope you are not misleading us and use a DataGridView


    Success
    Cor
    Monday, November 15, 2010 7:58 AM
  • Hi and thank you all for your answer.I am not using data binding. i want the user to select a row and pass the row to the calling form. frmLookUp is shown as dialog form -here is my code:

    Public

     

     

    Class frmLookUp

     

     

    Public strSelectedVal As String

     

     

    Public dtr As DataRow

     

     

    Private row As DataGridViewRow

     

     

     

    Public Function CloneWithValues(ByVal row As DataGridViewRow) _

     

     

    As DataGridViewRow

    CloneWithValues =

     

    CType(row.Clone(), DataGridViewRow)

     

     

    For index As Int32 = 0 To row.Cells.Count - 1

    CloneWithValues.Cells(index).Value = row.Cells(index).Value

     

     

    Next

     

     

    End Function

     

     

    Private Sub dtgLookUp_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgLookUp.DoubleClick

     

     

    Dim I As Integer

    I = dtgLookUp.CurrentRow.Index

     

     

    Dim d As DataRow

    d = dtgLookUp.SelectedRows.Item(I)

    dtgLookUp.CurrentRow.Clone()

    dtr = CloneWithValues(row)

     

     

    Me.Close()

     

     

    End Sub

    End

     

     

    Class


    hagita
    Monday, November 15, 2010 1:38 PM
  • Hey, no not using datagridview. I just pasted my code into Blair's reply. Thanks!!
    hagita
    Monday, November 15, 2010 1:39 PM
  • Thanks but its now what i need. i need the whole row. into a datarow object.
    hagita
    Monday, November 15, 2010 1:40 PM
  • What is the name of the control that your trying to get the selected row from?
     
    Wednesday, January 18, 2012 9:57 PM