使用过滤器时维护DataGridView复选框状态

发布于 2025-01-28 16:16:23 字数 2974 浏览 2 评论 0原文

有一个DataGridView,我通过My​​SQL查询填充了

Using command As New MySqlCommand("SELECT p_code as 'Prodcut Code', p_name as 'Product Name', filter " & _
                                      "from ordering_prc where isRetail = 0", conn)
        Using da As New MySqlDataAdapter(command)

            Dim dgvColumnHeaderStyle As New DataGridViewCellStyle() 'To center the headertext of the datagridview
            dgvColumnHeaderStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            dtgridorder.ColumnHeadersDefaultCellStyle = dgvColumnHeaderStyle

            da.Fill(dt)
PROD代码prod codeprodcoodbox
rockboxrow1a
checkboxrow2 row2 b2b row2b

include the inception header用于复选框列。通过编程添加“复选框”列,

            Dim CheckBox As New DataGridViewCheckBoxColumn
            CheckBox.Width = "20"
            CheckBox.Name = "CheckBox"
            CheckBox.HeaderText = " "
            'CheckBox.ReadOnly = True
            dtgridorder.Columns.Insert(0, CheckBox)

这很好。我将能够使用复选框填充我的datagrid,但无法实现我尝试做的事情 - 检查复选框时要维护复选框的检查状态。 因此,我搜索并找到 this

Dim dt As New DataTable
Dim source1 As New BindingSource()
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim str As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C# and VB Support\Example(VB)\Sample\Data1.mdf;Integrated Security=True"
    Dim sql As String = "select 0 as IsSelect,id,column1,column2 from test10"
    Dim con As New SqlConnection(str)
    Using cmd As New SqlCommand(sql, con)
        con.Open()
        Using Adapter As New SqlDataAdapter(cmd)
            Adapter.Fill(dt)
        End Using
        con.Close()
    End Using
    Dim view1 As New DataView(dt)
    source1.DataSource = view1
    DataGridView1.DataSource = view1
    DataGridView1.Refresh()

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    source1.Filter = "Column1 like '%" & TextBox1.Text & "%'"

End Sub

我尝试使用此方法,但与编程添加的复选框相处不佳。当我遵循选择0作为ISSELECT,ID,column1,column2 from test10并将列直接添加到我的datagridview时,结果看起来像是此

Isselectprod code code codeprod namefilt filter
0row1 arow1 a row1a row2
arow22ROW2B

如果我编辑0到1,则在我过滤datagrid时保持1个值,但不应该这样。复选框应出现。

有什么建议吗?

I have a datagridview which I populated thru mysql query

Using command As New MySqlCommand("SELECT p_code as 'Prodcut Code', p_name as 'Product Name', filter " & _
                                      "from ordering_prc where isRetail = 0", conn)
        Using da As New MySqlDataAdapter(command)

            Dim dgvColumnHeaderStyle As New DataGridViewCellStyle() 'To center the headertext of the datagridview
            dgvColumnHeaderStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            dtgridorder.ColumnHeadersDefaultCellStyle = dgvColumnHeaderStyle

            da.Fill(dt)
IncludeProd codeProd NameFilter
checkboxrow1row1a
checkboxrow2row2b

the include header is for the checkbox column. The checkbox column is programmatically added like this

            Dim CheckBox As New DataGridViewCheckBoxColumn
            CheckBox.Width = "20"
            CheckBox.Name = "CheckBox"
            CheckBox.HeaderText = " "
            'CheckBox.ReadOnly = True
            dtgridorder.Columns.Insert(0, CheckBox)

This works fine. I will be able to populate my datagrid with checkbox column but cannot achieve what I try to do -to maintain the checkstate of the checkbox column when I check the checkbox.
So I search and found this

Dim dt As New DataTable
Dim source1 As New BindingSource()
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim str As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C# and VB Support\Example(VB)\Sample\Data1.mdf;Integrated Security=True"
    Dim sql As String = "select 0 as IsSelect,id,column1,column2 from test10"
    Dim con As New SqlConnection(str)
    Using cmd As New SqlCommand(sql, con)
        con.Open()
        Using Adapter As New SqlDataAdapter(cmd)
            Adapter.Fill(dt)
        End Using
        con.Close()
    End Using
    Dim view1 As New DataView(dt)
    source1.DataSource = view1
    DataGridView1.DataSource = view1
    DataGridView1.Refresh()

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    source1.Filter = "Column1 like '%" & TextBox1.Text & "%'"

End Sub

I try this method but It won't go well with the programmatically added checkbox. and when I follow the select 0 as IsSelect,id,column1,column2 from test10 and add the columns directly to my datagridview the result is looks like this

isSelectProd codeProd NameFilter
0row1row1a
0row2row2b

If I edit the 0 to 1 it maintain the 1 value while I filtering the datagrid, but It shouldn't be like this. The checkbox should appear.

Any suggestions please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文