0

如果该行匹配,我正在尝试通过gridview中的行和文本中的颜色循环数据集的值。

下面的代码有效,但是每当我通过 更改页面PageIndexChanging并且再次运行此函数时,着色不再起作用。如果有匹配项,它仍会循环通过 gridview,但未显示效果。

        --variable initialization class instantiation--

        --code to connect to db here--

        mySQLCommand.CommandText = "SELECT ..."
        mySQLAdapter = New SqlDataAdapter(mySQLCommand)
        mySQLAdapter.Fill(myDataset)
        Me.MainPageGridView.DataSource = myDataset
        Me.MainPageGridView.DataBind()

        mySQLCommand.CommandText = "SELECT ... The ID's to be matched"
        mySQLAdapter = New SqlDataAdapter(mySQLCommand)
        mySQLAdapter.Fill(myDatasetNew)
        Me.MainPageGridView.DataSource = myDatasetNew

       For Each dataRow In myDataset.Tables(0).Rows
            thisID = dataRow("ID").ToString
            For Each gvRow In Me.MainPageGridView.Rows
                If gvRow.Cells(2).Text = thisID Then
                    For column = 0 To 14 Step 1
                        gvRow.Cells(column).ForeColor = Drawing.Color.RosyBrown
                    Next
                    Exit For
                End If
            Next
        Next
4

2 回答 2

2

为什么不使用MainPageGridView_RowDataBound事件来匹配 id?我已将您的原始代码重构为如下所示,请检查并告诉我它是否有效:

'In DataBind or some other method
        'Load(myDataSet)
        mySQLCommand.CommandText = "SELECT ..."
        mySQLAdapter = New SqlDataAdapter(mySQLCommand)
        mySQLAdapter.Fill(myDataset)

        'Load myDatasetNew and bind it to grid
        mySQLCommand.CommandText = "SELECT ... The ID's to be matched"
        mySQLAdapter = New SqlDataAdapter(mySQLCommand)
        mySQLAdapter.Fill(myDatasetNew)
        Me.MainPageGridView.DataSource = myDatasetNew
        Me.MainPageGridView.DataBind()

并在

Protected Sub MainPageGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MainPageGridView.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim id As String = DataBinder.Eval(e.Row.DataItem, "ID") 'The name of ID column in "myDatasetNew"

            Dim dv As System.Data.DataView = myDataset.Tables(0).DefaultView
            dv.RowFilter = "ID = " & id

            If dv.Count > 0 Then 'id matches
                'Change foreclor of entire row
                e.Row.ForeColor = Drawing.Color.RosyBrown
            End If

        End If
    End Sub
于 2011-08-04T04:16:53.613 回答
1

您确实需要在GridView.RowDataBound事件中进行数据比较。

于 2011-08-04T04:07:47.593 回答