如何在过滤VB.NET时维护datagridview复选框列checkstate [英] How do I maintain datagridview checkbox column checkstate while filtering VB.NET

查看:119
本文介绍了如何在过滤VB.NET时维护datagridview复选框列checkstate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个dgvw。它从sql数据库加载数据。现在,我向dgvw添加了一个复选框列。我的winform还有一个文本框,用于搜索/过滤datagridview。这是我的代码:



I have a dgvw.It loads data from an sql database.Now, i added a checkbox column to the dgvw.My winform also has a textbox that is used to search in/filter the datagridview. Here is my code:

Private Sub Contact_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    filterdata("")
Private Sub filterdata(valuetosearch As String)
    con.Open()
    Dim cmd As New SqlCommand("Select * from Contacts where CONCAT([Unique id],Prefix,[First name],[Last name],Gender,Title,Company,Phone,Mobile,Fax,[b.email],[p.email],Reference,Address,[Address 2],Country,City,Zip,Facebook,GooglePlus,Instagram,Twitter,Website,Salary,Currency,[Group],[Id/Status],Note,[Added by]) like '%" & valuetosearch & "%' ", con)
    Dim reader As SqlDataReader = cmd.ExecuteReader
    Dim dt As New DataTable
    dt.Load(reader)
    userdatagrid.AutoGenerateColumns = True
    userdatagrid.DataSource = dt
    userdatagrid.Refresh()
    con.Close()
End Sub
Private Sub searcgcon_TextChanged(sender As Object, e As EventArgs) Handles searcgcon.TextChanged
    If searcgcon.Text = "" Then
        filterdata("")
        entrylabel.Text = "There are/is " & userdatagrid.Rows.Count & " contact entries"
    Else
        filterdata(searcgcon.Text)
        entrylabel.Text = "There are/is " & userdatagrid.Rows.Count & " contact entries that contain your query :" & searcgcon.Text
    End If
End Sub





问题用这个代码是,假设我已经检查了一行的复选框然后我尝试使用textbox在文本框/过滤器中键入一些内容。但是一旦我开始键入之前检查过的复选框就变得未经检查。我知道我错了一些地方我从youtube和stackoverflow获得了这个代码。所以请帮助我。过滤时如何维护检查状态?



我尝试过:



在其他论坛上,我被告知,我的错误是我继续从实际数据库加载数据,但不是从过滤后的数据库加载数据。我显然不知道在代码中要做什么或改变什么。任何帮助都将不胜感激!



The problem with this code is, suppose i have checked a row's checkbox then i try to type something in the textbox/filter using the textbox.But as soon as i start typing the checkbox that was checked earlier becomes unchecked.I know i am wrong some where.I got this code from youtube and stackoverflow.So please help me..How do i maintain the checkstate while filtering ?

What I have tried:

On other forums, i was told that, my mistake is that i keep on loading data from the actual database but not from the filtered one. I clearly have no idea what to do or what to change in the code.Any help would be appreciated!

推荐答案

好的 - 好吧,如果你还没有,您必须首先在表单设计器或表单加载事件中设置
OK - well, if you haven't already, you must first set
userdatagrid.AllowUserToAddRows = False



把它放在你的新搜索按钮的Click事件中:

either in the form designer, or in the form load event.
The put this in your new search button's Click event:

Dim r As Integer, c As Integer
Dim bViz As Boolean = False
userdatagrid.CurrentCell = Nothing
For r = 0 To userdatagrid.Rows.Count - 1
   bViz = False
   For c = 0 To userdatagrid.Columns.Count - 1
      If userdatagrid.Rows(r).Cells(c).Value Like "*" & searcgcon.Text & "*" Then
         bViz = True
         Exit For
      End If
   Next
   userdatagrid.Rows(r).Visible = bViz
Next

用户可以只需清除文本框并再次搜索即可返回原始数据集 - 或者您可以提供单独的按钮来执行此操作。



当然,这样做,您可以简化原始数据绑定,删除不必要的过滤子句形式。

Users can get back to the original dataset by simply clearing the textbox and searching again - or you could provide a separate button to do that.

Of course, doing it this way, you can simplify your original databinding, removing the unnecessary filtering clause form that.


这篇关于如何在过滤VB.NET时维护datagridview复选框列checkstate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆