2015-06-20 52 views
1

我必须使用textbox过滤datagridview。下面的代码用于填充db类的gridview.getdata函数返回数据表。在Windows应用程序中的DataGridview中搜索VB.NET

我没有使用gridview的datasource属性,而是使用循环提交gridview。

我可以使用datasource属性和dataview进行搜索,但我没有直接从datasource属性填充datagridview。

Sub griddesgn() 
    DataGridView1.Columns.Clear() 
    DataGridView1.Rows.Clear() 
    DataGridView1.Columns.Add("crime", "crime") 
    DataGridView1.Columns.Add("actname", "actname") 
    DataGridView1.Columns.Add("section", "section") 
    DataGridView1.Columns.Add("description", "description") 
End Sub 

Private Sub TEST_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    griddesgn() 
    Dim DBOBJ As New db 
    Dim DTT As DataTable = DBOBJ.getdata("SELECT crime,actname,section,description from natureofcomplaint_women") 

    If DTT.Rows.Count > 0 Then 
     For i As Integer = 0 To DTT.Rows.Count - 1 
      DataGridView1.Rows.Add() 
      DataGridView1.Rows(i).Cells("crime").Value = DTT.Rows(i).Item("crime") & "" 
      DataGridView1.Rows(i).Cells("actname").Value = DTT.Rows(i).Item("actname") & "" 
      DataGridView1.Rows(i).Cells("section").Value = DTT.Rows(i).Item("section") & "" 
      DataGridView1.Rows(i).Cells("description").Value = DTT.Rows(i).Item("description") & "" 
     Next 
    End If 

End Sub 
+0

对不起,这对我来说还不清楚。你在标题中提到搜索,但你(隐含地)讨论在你的问题中使用'ItemsSource'。 –

+0

我编程地填充数据在gridview中使用for循环从datatable dt由getdata函数返回。现在数据应该过滤,只要我开始在显示的文本框中键入。它说... – user3449614

+2

绑定DataTable到DataGridView与BindingSource 。然后,使用BindingSource的Filter属性。 – Graffito

回答

0

使用WHERE语句在SQL查询

"SELECT crime,actname,section,description from natureofcomplaint_women WHERE crime = " & txtSearch.text 

(如果你想在犯罪搜索) 更改您的需求。 只是重复数据网格填充你上面使用,但是有一个altereted SQL查询每次输入/按搜索按钮

0

我仍然认为这是最好的结合...

Sub griddesgn() 
DataGridView1.Columns.Clear() 
DataGridView1.Rows.Clear() 
DataGridView1.Columns.Add("crime", "crime") 
DataGridView1.Columns.Add("actname", "actname") 
DataGridView1.Columns.Add("section", "section") 
DataGridView1.Columns.Add("description", "description") 
End Sub 

Private Sub TEST_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    griddesgn() 
    Dim DBOBJ As New db 
    Dim DTT As DataTable = DBOBJ.getdata("SELECT crime,actname,section,description from natureofcomplaint_women") 

    Dim source1 as New BindingSource() 
    source1.DataSource = DTT 
    source1.Filter = "crime = '" & textboxtCrime.text & "'" 
    DataGrideView1.DataSource = source1 

End Sub 
+0

它工作正常,但我必须应用一些格式化从datatable.thats我用于回路填充gridview返回的数据。 – user3449614

+0

好吧,如果您必须使用for循环,您可以在If块中包含它的复制部分,该块只包含符合所有条件的行:我将在下面的示例中以几分钟的形式发布另一个答案。 – DiscipleMichael

+0

请张贴您的答案。等待4我的回复 – user3449614

0
Sub griddesgn() 
    DataGridView1.Columns.Clear() 
    DataGridView1.Rows.Clear() 
    DataGridView1.Columns.Add("crime", "crime") 
    DataGridView1.Columns.Add("actname", "actname") 
    DataGridView1.Columns.Add("section", "section") 
    DataGridView1.Columns.Add("description", "description") 
End Sub 

Private Sub TEST_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    griddesgn() 
    Dim DBOBJ As New db 
    Dim DTT As DataTable = DBOBJ.getdata("SELECT crime,actname,section,description from natureofcomplaint_women") 

    If DTT.Rows.Count > 0 Then 
     For i As Integer = 0 To DTT.Rows.Count - 1 
     If (DTT.Rows(i).Item("Crime").Contains(txtCrimeFilter.Text) AND & _ 
       (DTT.Rows(i).Item("actname").Contains(txtActnameFilter.Text) AND & _ 
       (DTT.Rows(i).Item("section").Contains(txtSectionFilter.Text)  AND & _ 
       (DTT.Rows(i).Item("description").Contains(txtDescriptionFilter.Text) 
       DataGridView1.Rows.Add() 
       DataGridView1.Rows(i).Cells("crime").Value = DTT.Rows(i).Item("crime") & "" 
       DataGridView1.Rows(i).Cells("actname").Value = DTT.Rows(i).Item("actname") & "" 
       DataGridView1.Rows(i).Cells("section").Value = DTT.Rows(i).Item("section") & "" 
       DataGridView1.Rows(i).Cells("description").Value = DTT.Rows(i).Item("description") & "" 
     End If 
    Next 
End If 

结束Sub

+0

这假定你有一个过滤器文本框的每个4列......如果没有,然后删除它们。另外,上面的过滤器是区分大小写的......如果你不想这样做,那么就把它放到行上和textbox.text上。 – DiscipleMichael

+0

究竟是什么部分的要求不符合?它以编程方式填充datagridview并应用过滤器。 – DiscipleMichael

相关问题