在GridView中更改页面时检查行

时间:2017-05-12 19:42:59

标签: asp.net vb.net gridview

在GridView中单击寻呼机时,如果金额等于特定金额,我想突出显示每一行。首次填充GridView时,我有此工作,但每次单击寻呼机按钮移动到具有有效数量的下一页时,它都不会突出显示。有什么建议吗?

GridView的

    <asp:GridView ID="ERNDataGrid" runat="server" CssClass="clsGridView" AutoGenerateColumns="false" AllowPaging="true" PageSize="15"
        OnPageIndexChanging="OnPageIndexChanging" OnRowDataBound="OnRowDataBound" Width="99%">
        <HeaderStyle HorizontalAlign="Center" BackColor="#464646" Font-Bold="True" ForeColor="White"></HeaderStyle>
        <PagerSettings Mode="NextPreviousFirstLast" />
        <PagerStyle HorizontalAlign="Right" ForeColor="White" BackColor="#464646" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False"></PagerStyle>
        <AlternatingRowStyle BackColor="#DDDDDD"></AlternatingRowStyle>
        <Columns>
            <asp:BoundField DataField="CUID" HeaderText="Routing Number" ItemStyle-HorizontalAlign="Center" />
            <asp:BoundField DataField="Account" HeaderText="Account" ItemStyle-HorizontalAlign="Center" />
            <asp:BoundField DataField="Amount" HeaderText="Amount" DataFormatString="{0:C}" ItemStyle-HorizontalAlign="Center" />
            <asp:BoundField DataField="Serial" HeaderText="Check Number" ItemStyle-HorizontalAlign="Center" />
        </Columns>
    </asp:GridView>

VB.net Codebehind

Protected Sub ResearchGridView_Click(sender As Object, e As EventArgs) Handles ResearchGridView.Click
    strResearchAmount = txtERNResearchAmount.Value
    BindData()
End Sub

Private Sub ERNResearch_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        ERNDataGrid.DataBind()
    End If
End Sub

Protected Sub BindData()
    'Create a connection
    Dim myConnection As New SqlConnection("This works")
    'Create the command object, passing in the SQL string
    Const strSQL As String = "SELECT CUID, Account, Amount / 100 as Amount, Serial FROM [ACCU].[dbo].[ERN_ITEM_VIEW] Where Date = '04/13/2017' And CUID <> '0'"
    Dim myCommand As New SqlCommand(strSQL, myConnection)
    'Create the DataAdapter
    Dim myDA As New SqlDataAdapter()
    myDA.SelectCommand = myCommand
    'Populate the DataSet
    Dim myDS As New DataSet()
    myDA.Fill(myDS)
    'Set the datagrid's datasource to the dataset and databind
    ERNDataGrid.DataSource = myDS
    ERNDataGrid.DataBind()
    'Display Information on what page we are currently viewing
    'lblMessage.Text = "Viewing Page " & ERNDataGrid.PageIndex + 1 & " of " & ERNDataGrid.PageCount
End Sub

Protected Sub OnPageIndexChanging(sender As Object, e As GridViewPageEventArgs)
    ERNDataGrid.PageIndex = e.NewPageIndex
    Me.BindData()
End Sub

Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
    If e.Row.RowIndex > -1 Then
        If e.Row.Cells(2).Text = "$" & strResearchAmount Then
            e.Row.BackColor = Color.Yellow
        End If
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

您的代码看起来是正确的。每次调用OnRowDataBound时都会触发DataBind()事件。因为在OnPageIndexChanging中就是这种情况,RowDataBound应该触发。

strResearchAmount可能为空,因为只有在按一下按钮后才会填充它。由于页面索引更改也会触发PostBack,因此当第二次调用OnRowDataBound时,strResearchAmount可能为空。

检查strResearchAmount的值并确保它在PostBack中保持不变。