使用过滤器时维护DataGridView复选框状态
有一个DataGridView,我通过MySQL查询填充了
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 code | prod | coodbox |
---|---|---|---|
rockbox | row1 | 我 | a |
checkbox | row2 row2 b2 | b row2 | b |
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时,结果看起来像是此
Isselect | prod code code code | prod name | filt filter |
---|---|---|---|
0 | row1 a | row1 a row1 | a row2 |
a | row22 | ROW2 | B |
如果我编辑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)
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论