'Maintain Datagridview Checkbox State while using filter

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)
Include Prod code Prod Name Filter
checkbox row1 row1 a
checkbox row2 row2 b

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

isSelect Prod code Prod Name Filter
0 row1 row1 a
0 row2 row2 b

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?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source