gridview PageIndexChanging问题

发布于 2024-11-27 13:50:40 字数 1180 浏览 0 评论 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

I am trying to to loop through a dataset's value through the rows in a gridview and color in the text if that row matches.

The code below works however whenever I change the page through the PageIndexChanging and this function is ran again, the coloring doesn't work anymore. It still loops through the gridview if there is a match but the effects are not shown.

        --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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间不值得 2024-12-04 13:50:40

为什么不使用 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()

并在中执行 id 匹配

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

Why don't you use MainPageGridView_RowDataBound event to match the id? I have re-factored your original code to something like below, please check and let me know if it works:

'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()

and perform id matching in

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
十秒萌定你 2024-12-04 13:50:40

您确实需要在 GridView.RowDataBound 事件。

You really need to do your data comparison in the GridView.RowDataBound event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文