【问题标题】:gridview PageIndexChanging issuegridview PageIndexChanging 问题
【发布时间】:2011-08-04 02:54:21
【问题描述】:

我正在尝试通过 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

【问题讨论】:

  • 为什么要将两个数据集绑定到同一个网格?

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


【解决方案1】:

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

【讨论】:

  • 感谢回复,但是myDataset的加载在哪里呢?
  • 加载 myDatasetNew 之前的任何地方
  • 我无法让它与你的方法一起工作,所以我修改了一些代码以适应 rowdatabound,现在它可以工作了。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 2012-12-05
    • 1970-01-01
    相关资源
    最近更新 更多