Jassimi Jassimi - 9 months ago 130
Vb.net Question

Select full row on a DevExpress GridView depending on TextBox value

How do I select the full row on a

depending on

I tried the following code, but it's not working:

Dim rowHandle As Integer = GridView1.LocateByDisplayText(0, colC_Id, txtSelect.Text)
If rowHandle <> DevExpress.XtraGrid.GridControl.InvalidRowHandle Then
Dim row As System.Data.DataRow = GridView1.GetDataRow(GridView1.FocusedRowHandle)
row(0) = txtSelect.EditValue
End If

Answer Source

That code doesn't select a row in the GridControl. It does a case-sensitive search for the text entered in the text box in the GridColumn colC_Id and if a match is found, the text in the second column (column index 1) of the GridControl's data source is updated to the entered text in the row that is currently selected. The updated value will be reflected in the GridControl if that field from the data source has been added to the GridControl's GridColumn collection.

Here is an example of how to select a row in the GridControl based on a certain value where the GridControl's data source is a DataTable:

Dim myDataTable As DataTable = DirectCast(GridControl1.DataSource, DataTable)
Dim sFieldName As String = colC_Id.FieldName 
Dim sColumName As String = colC_Id.Name
Dim myDataRows As DataRow() = myDataTable.Select(sFieldName & "='" & TextBox1.Text.Replace("'", "''") & "'")

If myDataRows.Length > 0 Then
    Dim iDataSourceRowIndex As Int32 = myDataTable.IndexOf(myDataRows(0))
    GridView1.FocusedRowHandle = GridView1.GetRowHandle(iDataSourceRowIndex)
    MessageBox.Show("The text '" & TextBox1.Text & "' was not found in column '" & sColumName &"'")
End If