Vb.net datagridview with dataset as datasource

datagridviewdatasetvb.net

my question is, is there a way to filter records in the dataset and use that records to fill in the datagridview? for example, a datatable (with 3 columns: ID, StudentName, Gender) is filled with list of students. i have two datagrids in the form namely DatagridView1 and Datagridview2. DatagridView1 is where the the list of student where Gender is equal to M and DatagridView2 is where the the list of student where Gender is equal to F.

in my current solution, i am using a loop.

For each iStud as datarow in iDataset.Tables(0).Rows
      IF iStud.Item("Gender").ToString = "M" Then
            'add this record to DatagridView1
      Else
            'add this record to DatagridView2
      End If
Next

is there a way without using a loop?

Best Answer

Yes, there is. All you need to do is to filter the dataset using SELECT.

For example,

DatagridView1.Datasource = xSet.Tables("StudentList").SELECT("Gender = 'M'")
DatagridView2.Datasource = xSet.Tables("StudentList").SELECT("Gender = 'F'")

Brief Explanation:

xSet          is the name of the Dataset
StudentList   is the name of the Datatable
Gender        is the name of the Column where you want to filter

UPDATE

Screen Shot

Related Topic