Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何使用RowFilter在datagridview中突出显示行?_.net_Vb.net_Winforms_Datagridview_Datarowview - Fatal编程技术网

.net 如何使用RowFilter在datagridview中突出显示行?

.net 如何使用RowFilter在datagridview中突出显示行?,.net,vb.net,winforms,datagridview,datarowview,.net,Vb.net,Winforms,Datagridview,Datarowview,我正在使用RowFilter突出显示datagridview中的行,但当我清除该过滤器以查看所有记录时,它会删除我应用的突出显示: Sorter.DtSample.DefaultView.RowFilter = "FirstName = 'John'" For Each R0w In Sorter.DataGridView1.Rows R0w.defaultcellstyle.forecolor = Color.Red Sorter.DataGridView1(0, R0w.index

我正在使用RowFilter突出显示datagridview中的行,但当我清除该过滤器以查看所有记录时,它会删除我应用的突出显示:

Sorter.DtSample.DefaultView.RowFilter = "FirstName = 'John'" 
For Each R0w In Sorter.DataGridView1.Rows
  R0w.defaultcellstyle.forecolor = Color.Red
  Sorter.DataGridView1(0, R0w.index).Value = True
Next
Sorter.DtSample.DefaultView.RowFilter = ""

您可以使用
CellFormatting
RowPrepaint
evant对行应用某些格式。在这种情况下,检查触发事件的行的条件就足够了,并在需要时将格式应用于该行。(多亏了用于
RowPrePaint
的Proputoix,在这种情况下,它似乎比
cellFormatting
更快。)

由于该事件仅针对可见行引发,因此即使您有太多行,也不会出现性能问题。无论如何,让一个包含太多行的
DataGridView
不是一个好主意,在这种情况下,您应该使用类似于或分页的机制

示例

不管记录的数量有多少,下面是一个示例,如果您按
按钮1,我会将其
名字
以您在
TextBox1
中输入的文本开头的行着色。如果在
TextBox1
中输入空字符串并按
按钮1
所有行将显示为黑色

要使示例生效,您需要在表单上有一个
DataGridView1
TextBox1
按钮1

Public Class Form1
    Dim dt As DataTable
    Dim filter As String = ""
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        dt = New DataTable
        dt.Columns.Add("FirstName")
        dt.Columns.Add("LastName")
        dt.Rows.Add("John", "Doe")
        dt.Rows.Add("John", "Smith")
        dt.Rows.Add("Sara", "Allen")
        Me.DataGridView1.DataSource = dt
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        filter = Me.TextBox1.Text
        Me.DataGridView1.Invalidate()
    End Sub
    Private Sub DataGridView1_RowPrePaint(sender As Object, _
        e As DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
        If (e.RowIndex < 0 OrElse e.RowIndex = DataGridView1.NewRowIndex) Then Return
        Dim row = DataGridView1.Rows(e.RowIndex)

        If (String.IsNullOrEmpty(filter)) Then
            row.DefaultCellStyle.ForeColor = Color.Black
        Else
            Dim data = DirectCast(DataGridView1.Rows(e.RowIndex).DataBoundItem, _
                DataRowView).Row
            If data.Field(Of String)("FirstName").ToLower() _
                                                 .StartsWith(filter.ToLower()) Then
                row.DefaultCellStyle.ForeColor = Color.Red
            Else
                row.DefaultCellStyle.ForeColor = Color.Black
            End If
        End If
    End Sub
End Class
公共类表单1
作为数据表的Dim dt
Dim filter As String=“”
私有子表单1_Load(发送方作为对象,e作为事件参数)处理MyBase.Load
dt=新数据表
dt.列。添加(“名字”)
dt.Columns.Add(“LastName”)
dt.行。添加(“John”、“Doe”)
dt.行。添加(“约翰”、“史密斯”)
dt.行添加(“Sara”、“Allen”)
Me.DataGridView1.DataSource=dt
端接头
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
过滤器=Me.TextBox1.Text
Me.DataGridView1.Invalidate()
端接头
私有子DataGridView1_RowPrePaint(发送方作为对象_
e作为DataGridViewRowPrePaint事件参数)处理DataGridView1.RowPrePaint
如果(e.RowIndex<0或e.RowIndex=DataGridView1.NewRowIndex),则返回
Dim row=DataGridView1.Rows(例如RowIndex)
如果(String.IsNullOrEmpty(filter)),则
row.DefaultCellStyle.ForeColor=Color.Black
其他的
Dim data=DirectCast(DataGridView1.Rows(e.RowIndex).DataBoundItem_
DataRowView)。行
If data.Field(字符串)(“FirstName”).ToLower()_
.StartsWith(filter.ToLower())然后
row.DefaultCellStyle.ForeColor=Color.Red
其他的
row.DefaultCellStyle.ForeColor=Color.Black
如果结束
如果结束
端接头
末级
注意


如果将
RowFilter
应用于设置为
DataGridView
DataSource
的数据表,则只显示已过滤的行。所以我没有使用它,因为您想将过滤后的行着色为红色,而将其他行着色为黑色,所以我们需要显示所有行。

100K的记录太多了。不要在
DataGridView
中显示所有100K+记录。使用分页/筛选机制,只显示一些行。@RezaAghaei这并不真正符合我的目标。我需要能够滚动浏览所有记录,以将所选记录与未选记录(或未突出显示的记录)进行比较,因此请在
DataGridView
中使用。只要您继续交替使用所选、高亮显示、搜索和筛选,就永远不会得到明确的答案。它们有不同的含义;您处理数据的速度总是比在控件中生根更快。当您希望每个条件使某些行变为红色,并添加一些其他条件和其他附加条件时,请保持当前的红色行,根据新条件使某些新行变为红色,这意味着您需要
。你应该尝试一些东西,然后如果你面临一个问题,问一个新问题。开始点是创建一个
列表(Func(Of DataRow,Boolean))
并在
按钮1中单击
,而不是设置过滤器,添加一个
Func(Of DataRow,Boolean)
,然后在绘制中,检查一行的所有criteria,如果其中一个为真,则将行设为红色。