Using a column in a selected row - DataGrid
-
Hi, I am trying to select a row in a DataGrid by simply clicking on the row (any column in the row) which will in turn open a new form with various controls which are binded to the original record which was selected in the DataGrid. I have the following code which selects the whole row when clicked
Private Sub DataGridComplaints_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridComplaints.MouseUp Dim pt = New Point(e.X, e.Y) Dim hti As DataGrid.HitTestInfo = DataGridComplaints.HitTest(pt) If hti.Type = DataGrid.HitTestType.Cell Then DataGridComplaints.CurrentCell = New DataGridCell(hti.Row, hti.Column) DataGridComplaints.Select(hti.Row) End If End Sub
How do I use a column in the selected row to pass information into a variable which can then be used on the next form? Many thanks for any help you can give me. -
Hi, I am trying to select a row in a DataGrid by simply clicking on the row (any column in the row) which will in turn open a new form with various controls which are binded to the original record which was selected in the DataGrid. I have the following code which selects the whole row when clicked
Private Sub DataGridComplaints_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridComplaints.MouseUp Dim pt = New Point(e.X, e.Y) Dim hti As DataGrid.HitTestInfo = DataGridComplaints.HitTest(pt) If hti.Type = DataGrid.HitTestType.Cell Then DataGridComplaints.CurrentCell = New DataGridCell(hti.Row, hti.Column) DataGridComplaints.Select(hti.Row) End If End Sub
How do I use a column in the selected row to pass information into a variable which can then be used on the next form? Many thanks for any help you can give me.try this: var=DataGridComplaints.item(hti.Row,x) 'where x is the number of the column that you are trying to get data from Cosmin
-
Hi, I am trying to select a row in a DataGrid by simply clicking on the row (any column in the row) which will in turn open a new form with various controls which are binded to the original record which was selected in the DataGrid. I have the following code which selects the whole row when clicked
Private Sub DataGridComplaints_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridComplaints.MouseUp Dim pt = New Point(e.X, e.Y) Dim hti As DataGrid.HitTestInfo = DataGridComplaints.HitTest(pt) If hti.Type = DataGrid.HitTestType.Cell Then DataGridComplaints.CurrentCell = New DataGridCell(hti.Row, hti.Column) DataGridComplaints.Select(hti.Row) End If End Sub
How do I use a column in the selected row to pass information into a variable which can then be used on the next form? Many thanks for any help you can give me.You could use this logic also to select a row, which has been clicked ( I am not sure which has better performance or whether this logic has a drawback) : If DataGrid1.CurrentRowIndex > -1 then DataGrid1.Select(DataGrid1.CurrentRowIndex) End if I think to get column value of selected row, you should get it from the underlying DataSet (or DataTable) using the CurrentRowIndex of the DataGrid For Eg, if your DataSet ds has a table which is shown in the DataGrid, and you want to get value of column with index 3 of currently selected row, col3Value = ds.Tables(0).Rows(DataGrid1.CurrentRowIndex).Item(3) But this logic will fail if you have used the RowFilter to filter rows from the DataSet's default view. This is because the row index of DataGrid and that in the DataSet wont be same. To overcome that you could use this logic to get the DataRow in the DataSet corresponding to the row selected in the DataGrid: Private Function GetDataRow() As DataRow Dim dr() As DataRow = ds.Tables(0).Select(ds.Tables(0).DefaultView.RowFilter) Return dr(DataGrid1.CurrentRowIndex) End Function From the returned DataRow, you can take the required value : col3Value = dr.Item(3) Hope it helped. ---------------------------------------------------------------------------- "I think there is a world market for maybe 5 computers" Thomas Watson, chairman of IBM, 1943 "There is no reason anyone would want a computer in their home" Ken Olson, chairman & founder of Digital equipment, 1977 "This 'telephone' has too many shortcomings to be seriously considered as a means of communication. The device is ingerently of no value to us." Western Union internal memo, 1876 "640 K ought to be enough for anybody." Bill Gates, 1981 "Computers in the future may