c# - GridView 对行进行分组并更改颜色

标签 c# asp.net gridview

我使用可折叠的 GridView,带有父 GridView 和子 GridView 。在子 GridView 中,我希望按日期对行进行分组(SQL 查询已经这样做了),然后根据组为每组行交替颜色。

        <asp:GridView ID="gvSites" runat="server" AutoGenerateColumns="false" CssClass="Grid"
    DataKeyNames="Location" OnRowDataBound="OnRowDataBound">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <img alt = "" style="cursor: pointer" src="images/plus.png" />
                <asp:Panel ID="pnlPetrols" runat="server" Style="display: none">
                    <asp:GridView ID="gvPetrols" runat="server" AutoGenerateColumns="false" CssClass = "ChildGrid">
                        <Columns>
                            <asp:BoundField ItemStyle-Width="150px" DataField="Date" HeaderText="Date------------------------" />
                            <asp:BoundField ItemStyle-Width="150px" DataField="PatrolNo" HeaderText="PatrolNo----------------" />
                            <asp:BoundField ItemStyle-Width="150px" DataField="ScansDue" HeaderText="ScansDue----------------" />
                            <asp:BoundField ItemStyle-Width="150px" DataField="Total" HeaderText="Total" />
                        </Columns>
                    </asp:GridView>
                </asp:Panel>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField ItemStyle-Width="150px" DataField="Location" HeaderText="Location" />

    </Columns>
</asp:GridView>

这是我得到的:

Date        PatrolNo    ScansDue   Total
07/22/2014  2           8          1
07/22/2014  4           8          1
07/22/2014  6           8          1
07/22/2014  7           8          1
07/23/2014  6           8          2
07/23/2014  7           8          1
07/23/2014  8           8          1

我想要 07/22/2014 的所有记录都用一种颜色,所有 07/23/2014 的所有记录都用另一种颜色。

最佳答案

这将适用于在您的组之间改变的样式。

设置一个全局变量来保存当前的 css 类,并为行分组值。然后使用RowDatabound实现交替行

//Global variables
string currentClass = "alternateDataRow";
string currentGroup = string.Empty;

protected void gvPetrols_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //Get value from cell, you could also use DataKeys
        //We will assume 2 CSS classes "dataRow", "alternateDataRow"
        string rowGroup = e.Row.Cells[0].Text;

        //swap class if group value changes
        if (rowGroup != currentGroup)
        {
            currentClass = currentClass == "dataRow" ? "alternateDataRow" : "dataRow";
            currentGroup = rowGroup;
        }

        e.Row.CssClass = currentClass;

    }
}

现在您只需创建两个 CSS 类。

关于c# - GridView 对行进行分组并更改颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24933651/

相关文章:

c# - 将 Cmd 命令传递给 C# 应用程序

c# - 使用扩展方法添加事件

c# - 变量赋值和读取是原子操作吗?

asp.net - wkhtmltopdf 登录asp.net web app

c# - 在 ASP.NET 5 (MVC6) 中请求 BinaryRead

audio - Yii-Gridview在每个数据行中都有音频播放器

javascript - 如何在asp gridview中使用<EmptyDataTemplate>

c# - 在 Unity.MVC4 或 Unity(3.0) 中找不到 PerRequestLifetimeManager 类

javascript - Asp.Net按钮onclientclick问题

c# - GridView 中的超链接列