gridview PageIndexChanging问题

时间:2011-08-04 02:54:21

标签: asp.net vb.net gridview page-index-changed

如果该行匹配,我试图通过网格视图中的行和文本中的颜色遍历数据集的值。

下面的代码可以正常工作,每当我通过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

2 个答案:

答案 0 :(得分: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()

并在

中执行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

答案 1 :(得分:1)

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