如何使用GridView在单个单元格中创建多行

时间:2014-01-04 06:45:08

标签: html asp.net vb.net

我在这里面临一项任务...... 在我的gridview中有两个cols ok,命名为user和profile。我需要在单个单元格(多行)中显示与用户相关的配置文件列表。我怎么做??? 这就是我试过的

      <asp:BoundField DataField="UserName" HeaderText="User">
                        <HeaderStyle HorizontalAlign="Left" />
                        <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>
                    <%--<asp:BoundField DataField="ProfileName" HeaderText="Profile">
                        <HeaderStyle HorizontalAlign="Left" />
                        <ItemStyle HorizontalAlign="Left" />
                    </asp:BoundField>--%>
                    <asp:TemplateField>
                        <HeaderTemplate>Profile</HeaderTemplate>
                        <ItemTemplate>
                            <%#Eval("UserName.ProfileName")%>
                        </ItemTemplate>
                    </asp:TemplateField>

1 个答案:

答案 0 :(得分:0)

根据条件合并行。这是一个很好的例子

http://www.codeproject.com/Articles/34337/How-to-merge-cells-with-equal-values-in-a-GridView

public static void MergeRows(GridView gridView)
{
    for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
    {
        GridViewRow row = gridView.Rows[rowIndex];
        GridViewRow previousRow = gridView.Rows[rowIndex + 1];

        for (int i = 0; i < row.Cells.Count; i++)
        {
            if (row.Cells[i].Text == previousRow.Cells[i].Text)
            {
                row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 : 
                                       previousRow.Cells[i].RowSpan + 1;
                previousRow.Cells[i].Visible = false;
            }
        }
    }
}