Asp.net gridview页面索引交换问题
我试图通过gridview中的行循环遍历数据集的值,如果该行匹配,则返回文本中的颜色 但是,每当我通过Asp.net gridview页面索引交换问题,asp.net,vb.net,gridview,page-index-changed,Asp.net,Vb.net,Gridview,Page Index Changed,我试图通过gridview中的行循环遍历数据集的值,如果该行匹配,则返回文本中的颜色 但是,每当我通过PageIndexChanging更改页面并且再次运行此函数时,下面的代码都会起作用,颜色不再起作用。如果存在匹配项,它仍会在gridview中循环,但不会显示效果 --variable initialization class instantiation-- --code to connect to db here-- mySQLComman
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
您确实需要在事件中进行数据比较。您确实需要在事件中进行数据比较。为什么不使用
MainPageGridView\u 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
为什么不使用
MainPageGridView\u 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,它现在可以工作了。感谢您的回复,但是我在哪里加载myDataset
?在加载mydatasetnew之前的任何地方我都无法让它与您的方法一起工作,所以我修改了一点代码以适应rowdatabound,现在它可以工作了。