如何在GridView中合并行 [英] How to merge rows in gridview

查看:50
本文介绍了如何在GridView中合并行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在gridview中有一些数据,如下所示.

I have some data in gridview as below.

我想按以下方式合并gridview中的行.

I want to merge the rows in gridview as below.

我已经尝试在RowDataBound()和PreRender()中使用以下代码,结果与我想要的不一样.

I already tried with below code in RowDataBound() and PreRender(), the result is not the same as I want.

for (int rowIndex = GridView1.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
    GridViewRow gvRow = GridView1.Rows[rowIndex];
    GridViewRow gvPreviousRow = GridView1.Rows[rowIndex + 1];
    for (int cellCount = 0; cellCount < gvRow.Cells.Count; cellCount++)
    {
        if (gvRow.Cells[cellCount].Text ==
                            gvPreviousRow.Cells[cellCount].Text)
        {
            if (gvPreviousRow.Cells[cellCount].RowSpan < 2)
            {
                gvRow.Cells[cellCount].RowSpan = 2;
            }
            else
            {
                gvRow.Cells[cellCount].RowSpan =
                    gvPreviousRow.Cells[cellCount].RowSpan + 1;
            }
            gvPreviousRow.Cells[cellCount].Visible = false;
        }
    }
}

在aspx中,

<asp:GridView ID="GridView1" runat="server" Width="100%"
    AutoGenerateColumns = "false" AlternatingRowStyle-BackColor = "#fffccc" HeaderStyle-ForeColor ="#ffffff" 
    HeaderStyle-BackColor = "#333" AllowPaging ="true" OnRowDataBound="GridView1_RowDataBound" PageSize = "20"
    OnPageIndexChanging="GridView1_PageIndexChanging" OnPreRender="GridView1_PreRender">
    <HeaderStyle Height="30px" />
        <Columns>
            <asp:TemplateField HeaderText="Client Company">
                <ItemTemplate>
                    <asp:Label ID="lblClientCompany" runat="server" Text='<%# Eval("ClientCompany")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Position">
                <ItemTemplate>
                    <asp:Label ID="lblPosition" runat="server" Text='<%# Eval("Position")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Candidate">
                <ItemTemplate>
                    <asp:Label ID="lblCandidate" runat="server" Text='<%# Eval("Candidate")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Status">
                <ItemTemplate>
                    asp:Label ID="lblStatus" runat="server" Text='<%# Eval("Status")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    <AlternatingRowStyle BackColor="#fffccc"  />
</asp:GridView>

请帮助我,因为我不知道.谢谢.

Please help me becasue I have no idea. Thanks.

推荐答案

您需要尝试使用 DataBound 中的方法,而不是 RowDataBound

You need to try the method in DataBound not RowDataBound

DataBound 在GridView控件绑定到数据源后(在所有行都绑定之后)触发.

DataBound fires after the GridView control binds to a data source(after all rows bound).

RowDataBound .

ASPX

<asp:GridView ID="GridView1" runat="server" Width="100%"
    AutoGenerateColumns = "false" AlternatingRowStyle-BackColor = "#fffccc" HeaderStyle-ForeColor ="#ffffff" 
    HeaderStyle-BackColor = "#333" AllowPaging ="true"  OnDataBound="GridView1_DataBound1" PageSize = "20"
    OnPageIndexChanging="GridView1_PageIndexChanging">
    <HeaderStyle Height="30px" />
        <Columns>
            <asp:TemplateField HeaderText="Client Company">
                <ItemTemplate>
                    <asp:Label ID="lblClientCompany" runat="server" Text='<%# Eval("ClientCompany")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Position">
                <ItemTemplate>
                    <asp:Label ID="lblPosition" runat="server" Text='<%# Eval("Position")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Candidate">
                <ItemTemplate>
                    <asp:Label ID="lblCandidate" runat="server" Text='<%# Eval("Candidate")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Status">
                <ItemTemplate>
                    asp:Label ID="lblStatus" runat="server" Text='<%# Eval("Status")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    <AlternatingRowStyle BackColor="#fffccc"  />
</asp:GridView>

隐藏代码

protected void GridView1_DataBound1(object sender, System.EventArgs e)
{
    for (int rowIndex = GridView1.Rows.Count - 2; rowIndex >= 0; rowIndex--)
    {
        GridViewRow gvRow = GridView1.Rows[rowIndex];
        GridViewRow gvPreviousRow = GridView1.Rows[rowIndex + 1];
        for (int cellCount = 0; cellCount < gvRow.Cells.Count; cellCount++)
        {
            if (gvRow.Cells[cellCount].Text ==
                                gvPreviousRow.Cells[cellCount].Text)
            {
                if (gvPreviousRow.Cells[cellCount].RowSpan < 2)
                {
                    gvRow.Cells[cellCount].RowSpan = 2;
                }
                else
                {
                    gvRow.Cells[cellCount].RowSpan =
                        gvPreviousRow.Cells[cellCount].RowSpan + 1;
                }
                gvPreviousRow.Cells[cellCount].Visible = false;
            }
        }
    }
}

这篇关于如何在GridView中合并行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆