Asp.Net网格视图。将最后一列移动到新行并跨越其他列

时间:2014-07-22 16:29:53

标签: c# asp.net gridview

是否可以在网格视图控件中将最后一列移动到新行并让新行跨越前一列?

类似的东西:

Col1 Col2 Col3 Col4 Col5
Col6
Col1 Col2 Col3 Col4 Col5
Col6 

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码来获取您要求的格式。查看最后一个<asp:TemplateField>,它需要使用代码将最后一列移动到新行。

enter image description here

<asp:GridView ID="GridView1" runat="server" AllowSorting="false"
            AutoGenerateColumns="False" CellPadding="0"
            ForeColor="#333333" GridLines="None">
            <Columns>
                <asp:BoundField DataField="Col1" HeaderText="Col 1" ItemStyle-Width="50px" />
                <asp:BoundField DataField="Col2" HeaderText="Col 2" ItemStyle-Width="50px" />
                <asp:BoundField DataField="Col3" HeaderText="Col 3" ItemStyle-Width="50px" />
                <asp:BoundField DataField="Col4" HeaderText="Col 4" ItemStyle-Width="50px" />
                <asp:BoundField DataField="Col5" HeaderText="Col 5" ItemStyle-Width="50px" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <tr>
                            <td colspan="100%">
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Col6") %>'></asp:Label>
                                <itemstyle width="100%" />
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <HeaderStyle CssClass="headerstyle" />
</asp:GridView>

我用于gridview绑定的数据源对象

public class GridViewColumns
{
    public String Col1 { get; set; }
    public String Col2 { get; set; }
    public String Col3 { get; set; }
    public String Col4 { get; set; }
    public String Col5 { get; set; }
    public String Col6 { get; set; }
}

您的PageLoad

if (!Page.IsPostBack)
{
        var datasource = new List<GridViewColumns>() {
        new GridViewColumns { 
            Col1 = "Col 1",
            Col2 = "Col 3",
            Col3 = "Col 3",
            Col4 = "Col 4",
            Col5 = "Col 5",
            Col6 = "Col 6"
        },
        new GridViewColumns { 
            Col1 = "Col 1",
            Col2 = "Col 3",
            Col3 = "Col 3",
            Col4 = "Col 4",
            Col5 = "Col 5",
            Col6 = "Col 6"
        }
      };

      GridView1.DataSource = datasource;
      GridView1.DataBind();
}