简体   繁体   English

DataGridView检查所有行vb.net

[英]DataGridView check all Rows vb.net

Private Sub comboBoxStudentID_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comboBoxStudentID.SelectedIndexChanged
    For Each dr As DataGridViewRow In Me.dataGridViewStudent.Rows

        If dr.Cells(0).Value.ToString.Contains(comboBoxStudentID.Text) Then dr.Visible = True Else dr.Visible = False


    Next
End Sub

I've created this method to check and display a row that contains the same Student ID as selected from the comboBoxStudentID the problem is it only checks the first row of the DataGridView. 我创建了此方法来检查并显示包含与从comboBoxStudentID中选择的学生ID相同的学生ID的行,问题是它仅检查DataGridView的第一行。 How can I make it check all of the rows if there is match of Student ID? 如果学生ID匹配,如何检查所有行?

Without knowing how your data is organized, you could do this by binding the DataGridView and the ComboBox to the same data source. 在不知道数据如何组织的情况下,可以通过将DataGridViewComboBox绑定到同一数据源来完成此操作。 Since this is not specified and looking at the snippet of code (which appears to work) it appears to loop through the rows and if the currently selected combo box text is contained in the first column cell, then make that row visible. 由于未指定此项,而是查看代码片段(似乎起作用),因此似乎在各行之间循环,并且如果第一列单元格中包含当前选定的组合框文本,则使该行可见。 Otherwise make the row invisible. 否则,使该行不可见。

The posted code appears to work however there is one scenario you may want to consider. 发布的代码似乎可以正常工作,但是您可能需要考虑一种情况。 If the combo box contains the values that are in column one of the DataGridView , THEN, when the user selects a value in the combo box, the DataGridView will display ONLY those values that match the current combo box selection. 如果组合框包含在DataGridView列中的值,则当用户在组合框中选择一个值时, DataGridView将仅显示与当前组合框选择匹配的那些值。 This appears to be what you are trying to do. 这似乎是您要尝试执行的操作。

The problem is how do you re-display all the rows? 问题是如何重新显示所有行?

Once the combo box selection has changed to some value, the user can delete the selection but all the rows will not display unless you handle another event. 组合框选择更改为某个值后,用户可以删除选择,但是除非您处理其他事件,否则所有行都不会显示。 Also this strategy is not very user intuitive. 同样,这种策略不是很直观。 One possible solution (below) simply adds a top item of “Reset” to the top of the combo boxes items. 一个可能的解决方案(如下)仅在组合框项目的顶部添加了“重置”的顶部项目。 Then when the user selects “Reset” all row will become visible. 然后,当用户选择“重置”时,所有行将变为可见。 Hope this helps. 希望这可以帮助。

To reiterate: you should consider setting this up with a DataSource and possibly a BindingSource where this is done for you automatically. 重申一下:您应该考虑使用DataSource以及可能的BindingSource ,在此情况下将自动为您完成设置。 Just a thought. 只是一个想法。

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
  If ComboBox1.SelectedIndex > 0 Then
    For Each dr As DataGridViewRow In DataGridView1.Rows
      If (Not dr.IsNewRow) Then
        If dr.Cells(0).Value.ToString.Contains(ComboBox1.Text) Then
          dr.Visible = True
        Else
          dr.Visible = False
        End If
      End If
    Next
  Else
    For Each row As DataGridViewRow In DataGridView1.Rows
      row.Visible = True
    Next
  End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM