2012-01-13 102 views
1

我的問題是,有沒有辦法來過濾記錄在數據集中,並使用該記錄填寫datagridview?例如,一個數據表(有3列:ID,StudentName,Gender)填充了學生列表。我有兩個數據網格的形式,即DatagridView1Datagridview2DatagridView1是其中Gender等於MDatagridView2的學生列表,其中Gender等於F的學生列表在哪裏。vb.net datagridview數據集作爲數據源

在我目前的解決方案中,我正在使用一個循環。

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 

有沒有方法沒有使用循環?

回答

4

是的,有。您只需使用SELECT即可過濾數據集。

例如,

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

簡要說明:

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

+0

謝謝你,約翰! – 2012-01-13 06:07:20