Link to home
Start Free TrialLog in
Avatar of b001
b001Flag for Afghanistan

asked on

SEARCHING DATATABLE

Hi Experts
The following code
   DA.Fill(DS, "Codes")
        dt = DS.Tables("codes")
       grid2.DataSource = dt
shows:-
             id                     desc                      qty
          P113                   PADS                       2
          P114                   PADS                       5
          P115                   PADS                       6
I would like to only show middle line in grid3
and like to store id (P114) to veriable idx
                           qty(5)     to              qtyx

Thanks
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Dim dt2 As New Data.DataTable
Dim idx As String = String.Empty
Dim qtyx As String = String.Empty
Dim dr As Data.DataRow
Dim dr2 As Data.DataRow
For Each dr In dt.Rows
     If String.Compare(dr.Item(0), "P114", true) = 0 Then
          idx = dr.Item(0)
          qtyx = dr.Item(2)
          dr2 = dt2.NewRow()
          dr2.Item(0) = dr.Item(0)
          dr2.Item(1) = dr.Item(1)
          dr2.Item(2) = dr.Item(2)
          dt2.Rows.Add(dr2)
     End If
Next
grid3.DataSource = dt2
Use RowFilter to filter rows in gridView
   DA.Fill(DS, "Codes")
        dt = DS.Tables("codes")
      dt.DefaultView.RowFilter = "id='P114'"
       grid2.DataSource = dt
 
now grid will show:-
             id                     desc                      qty
         P114                   PADS                       5
 
you can also retreive values of id and qty as follows:
       Dim idx as string
       Dim qtyx as integer
       idx = dt.DefaultView(0)(0)
       qtyx = dt.DefaultView(0)(1)

because dt.DefaultView contains only one row, the filtered row. That's why I wrote dt.DefaultView(0).
Avatar of b001

ASKER

HI guramrit:
Thanks it works ok
 
      dt.DefaultView.RowFilter = "id='P114'"

What about if P114 is stored to veriable IDX
would the code chage to

      dt.DefaultView.RowFilter = "id='IDX'"

or something different

Thanks
ASKER CERTIFIED SOLUTION
Avatar of guramrit
guramrit
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial