【问题标题】:Highlight DataGrid Row if Cell Equals X如果单元格等于 X,则突出显示 DataGrid 行
【发布时间】:2017-10-11 01:44:18
【问题描述】:

我正在使用绑定到由我的 SQLDataAdapter 填充的 DataSet 的 asp:DataGrid。在我的页面上显示 asp:DataGrid 之前,我想检查 asp:DataGrid,对于每个等于 $10.00 的金额,我想突出显示黄色行。

这是我的 VB CodeBehind

    'Create a connection
    Dim myConnection As New SqlConnection("Yes, 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.CurrentPageIndex + 1 & " of " & ERNDataGrid.PageCount

这是我的 asp:DataGrid

    <asp:DataGrid ID="ERNDataGrid" runat="server" BorderWidth="0px"
        CellPadding="2" Width="100%"
        Font-Name="Verdana"
        Font-Size="Smaller"
        AutoGenerateColumns="False"
        HeaderStyle-HorizontalAlign="Center"
        HeaderStyle-Font-Bold="True"
        HeaderStyle-BackColor="#77a13d"
        HeaderStyle-ForeColor="White"
        AlternatingItemStyle-BackColor="#dddddd"
        AllowPaging="True"
        PageSize="15"
        OnPageIndexChanged="ERNDataGrid_PageIndexChanged" Font-Names="Verdana">

        <HeaderStyle HorizontalAlign="Center" BackColor="#464646" Font-Bold="True" ForeColor="White"></HeaderStyle>

        <PagerStyle Mode="NextPrev" HorizontalAlign="Right"
            ForeColor="White" BackColor="#464646"
            NextPageText="Next Page >>" PrevPageText="<< Prev. Page" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False"></PagerStyle>

        <AlternatingItemStyle BackColor="#DDDDDD"></AlternatingItemStyle>

        <Columns>
            <asp:BoundColumn HeaderText="Routing Number" DataField="CUID" ItemStyle-HorizontalAlign="Center" ReadOnly="True">
                <ItemStyle HorizontalAlign="Center"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn HeaderText="Account" DataField="Account" ItemStyle-HorizontalAlign="Center" ReadOnly="True">
                <ItemStyle HorizontalAlign="Center"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn HeaderText="Amount" DataField="Amount" ItemStyle-HorizontalAlign="Center" ReadOnly="True" DataFormatString="{0:C}">
                <ItemStyle HorizontalAlign="Center"></ItemStyle>
            </asp:BoundColumn>
            <asp:BoundColumn HeaderText="Check Number" DataField="Serial" ItemStyle-HorizontalAlign="Center" ReadOnly="True">
                <ItemStyle HorizontalAlign="Center"></ItemStyle>
            </asp:BoundColumn>
        </Columns>

    </asp:DataGrid>

【问题讨论】:

    标签: asp.net vb.net datagrid


    【解决方案1】:

    DataGrid 具有您可以使用的 ItemDataBound 事件。

    protected void ERNDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item)
        {
            //cast the dataitem back to a datarowview 
            DataRowView row = e.Item.DataItem as DataRowView;
    
            //check the column value and color the row
            if (Convert.ToDecimal(row["amount"]) == 10)
            {
                e.Item.BackColor = Color.Red;
            }
        }
    }
    

    VB

    Protected Sub ERNDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
        If (e.Item.ItemType = ListItemType.Item) Then
    
            'cast the dataitem back to a datarowview 
            Dim row As DataRowView = CType(e.Item.DataItem,DataRowView)
    
            'check the column value and color the row
            If (Convert.ToDecimal(row("amount")) = 10) Then
                e.Item.BackColor = Color.Red
            End If          
    
        End If        
    End Sub
    

    【讨论】:

    • 感谢您的回答。在您回答之前,我咬紧牙关,将我的 DataGrid 更改为 GridView 并进行了第一个响应。经过一些调整,将属性更改为新名称后,我能够从 GridView 中获得我想要的结果。虽然我确信这个输出可能会起作用,但将所有内容改回来看看它是否会起作用,这可能需要我一些时间。无论哪种方式,我都会将此答案标记为有用,因为它是我最初正在寻找的。​​span>
    • 没问题。但我保证它有效。我在发布之前测试了所有内容。
    【解决方案2】:

    编辑: 我刚刚注意到您使用的是 DataGrid,而不是 GridView。我建议在按照我的示例之前将其更改为 GridView。

    为您的网格视图使用 OnRowDataBound 事件。

    因此,在您的 gridview 中生成每一行时,您运行检查以查看该行的金额是否等于 10 美元。

    Protected Sub ERNDataGrid_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles ERNDataGrid.RowDataBound
        If e.Row.Cells(2).Text = "$10" Then
            e.Row.BackColor = Drawing.Color.Yellow
        End If
    End Sub
    

    更改代码以适合您的规范和数据类型。

    【讨论】:

    • 我硬着头皮换成了GridView。我相信我的一部分是固执的,我的一部分是“害怕”,因为我在这方面还很陌生。我确实必须添加一个 RowIndex 检查。 code Private Sub ERNDataGrid_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles ERNDataGrid.RowDataBound If e.Row.RowIndex &gt; -1 Then If e.Row.Cells(2).Text = "$500.00" Then e.Row.BackColor = Drawing.Color.Yellow End If End If End Sub 尽管如此,我还是赢了,我能够得到我想要的结果。谢谢你,@Wenadin。
    最近更新 更多