datagridview selected row
hi all.....
how can i get the selected row when the user double click on any column of that row....for example there is an item list which display in datagridview......and each record contain multiple atrributes or column......and what i want is when the user double click on one of the attributes, it will return me that selected record...
does anyone have a solution in solving this problem ?
thanks
[426 byte] By [
goh6613] at [2007-12-23]
ya u are able to do this...
select the value of the datagrid first column....
or u can select the unique key in ur database. for it..
and check out all the detail related to it.. where u want
Example - to demonstrate general principle.
This uses full row selected and verifies the selectedrows collection. If you use a different selectionmode then you may need to iterate through the selectedcells collection which will give you the row and column indexes of the currently selected cells.
You probably have to turn off sorting for columns as clicking on column header will normally result in a sort.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
Me.Column1.SortMode = DataGridViewColumnSortMode.NotSortable
Me.Column2.SortMode = DataGridViewColumnSortMode.NotSortable
End Sub
''' <summary>
''' This will iterate through the currently selected row when a cell header is clicked.
''' DOuble clicking on other cells in the grid has no effect.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub DataGridView1_CellContentDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentDoubleClick
If e.RowIndex = -1 Then '//Column Header
Dim s As String = ""
For Each dr As DataGridViewRow In Me.DataGridView1.SelectedRows
For Each s1 As DataGridViewCell In dr.Cells
s = s & s1.Value & vbCrLf
Next
Next
MsgBox("All Items from selected row" & vbCr & s)
End If
End Sub
End Class
If you want to get the current row for the currently selected cell - then this can be achieved using something like - in a CellEnter event - as I'm getting only Item 0 - this is with an assumption that multi-select is set to False so there is only ever 1 item selected able to be selected. If you want more then you'll need to iterate over the selectedcells collection.
Dim
dr As DataRow = Me.DataGridView1.Rows(Me.DataGridView1.SelectedCells(0).RowIndex).DataBoundItem