2011-08-04 77 views
0

我想通過gridview中的行和文本中的行如果該行匹配來遍歷數據集的值。gridview PageIndexChanging問題

下面的代碼正常工作,但無論何時我通過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 
+0

你爲什麼要結合兩個數據集,以相同的網格? – Waqas

回答

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

感謝您的回覆,但我在哪裏可以加載'myDataset'? –

+0

加載myDatasetNew之前的任何地方 – Waqas

+0

我無法讓它與您的方法一起工作,所以我修改了一小段代碼以適合rowdatabound,現在它可以工作。 –