我使用的是Visual Studio 2017, SQL Server 2016。问题是,我已经在我的DataGridView中添加了复选框列,但它重复了5次
我已经从下面的链接的代码。它给出了结果,但为什么要重复复选框列呢?在组合框SelectedIndexChanged我调用一个函数,所以有什么问题吗?我该怎么办?
Private Sub GetRecords()
Dim query As String = " select distinct t.CPTCode,t.ServiceName,t.DepartmentName,t.SubName,t.Name as Company,t.bedcatename,t.ServiceCharge "
query += " from tariffpsri t "
query += " inner join psrirate p on t.Validid= p.Validid "
query += " inner join ItemOfService i on t.ServiceId = i.ServiceId "
query += " where t.DepartmentName= '" + ComboBox2.Text + "' "
query += " And t.companyid = 1 And i.Active = 1 "
query += " And Convert(varchar, p.ValidFrom, 111) = '" + ComboBox1.Text + "' "
query += " order by CPTCode "
conn = GetConnect()
conn.Open()
Using cmd As SqlCommand = New SqlCommand(query, conn)
'MsgBox(query)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using ds As New DataSet1
Using dt As DataTable = New DataTable()
sda.Fill(dt)
DataGridView1.DataSource = dt
End Using
End Using
End Using
End Using
conn.Close()
Dim headerCellLocation As Point = Me.DataGridView1.GetCellDisplayRectangle(0, -1, True).Location
'Place the Header CheckBox in the Location of the Header Cell.
headerCheckBox.Location = New Point(headerCellLocation.X + 8, headerCellLocation.Y + 2)
headerCheckBox.BackColor = Color.White
headerCheckBox.Size = New Size(18, 18)
'Assign Click event to the Header CheckBox.
AddHandler headerCheckBox.Click, AddressOf HeaderCheckBox_Clicked
DataGridView1.Controls.Add(headerCheckBox)
'Add a CheckBox Column to the DataGridView at the first position.
Dim checkBoxColumn As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
checkBoxColumn.HeaderText = ""
checkBoxColumn.Width = 30
checkBoxColumn.Name = "checkBoxColumn"
DataGridView1.Columns.Insert(0, checkBoxColumn)
'DataGridView1.Columns.Add(checkBoxColumn)
'Assign Click event to the DataGridView Cell.
AddHandler DataGridView1.CellContentClick, AddressOf DataGridView_CellClick
End Sub
Private Sub HeaderCheckBox_Clicked(ByVal sender As Object, ByVal e As EventArgs)
'Necessary to end the edit mode of the Cell.
DataGridView1.EndEdit()
'Loop and check and uncheck all row CheckBoxes based on Header Cell CheckBox.
For Each row As DataGridViewRow In DataGridView1.Rows
Dim checkBox As DataGridViewCheckBoxCell = (TryCast(row.Cells("checkBoxColumn"), DataGridViewCheckBoxCell))
checkBox.Value = headerCheckBox.Checked
Next
End Sub
Private Sub DataGridView_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
'Check to ensure that the row CheckBox is clicked.
If e.RowIndex >= 0 AndAlso e.ColumnIndex = 0 Then
'Loop to verify whether all row CheckBoxes are checked or not.
Dim isChecked As Boolean = True
For Each row As DataGridViewRow In DataGridView1.Rows
If Convert.ToBoolean(row.Cells("checkBoxColumn").EditedFormattedValue) = False Then
isChecked = False
Exit For
End If
Next
headerCheckBox.Checked = isChecked
End If
End Sub
Am calling above sub on combobox1 and combobox2 selectedindexchange
https://www.aspsnippets.com/Articles/Check-all-and-Uncheck-all-CheckBox-in-DataGridView-in-Windows-Application-using-C-and-VBNet.aspx
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/CSzdM.png
在没有看到您写的代码的情况下,我可以给出一个建议,那就是在
中设置一个断点BindGrid()
sub查看是否被多次调用。
由于它在子例程中添加了复选框,如果它被多次调用,它将为每次调用添加一个复选框。
如果你能分享你写的代码,我们也许能提供更详细的帮助。