将gridview列显示为行

时间:2011-02-04 15:08:32

标签: asp.net vb.net

 <asp:GridView ID="GridView2" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                               DataSourceID="opendiarysource" Font-Size="Small" style="font-size: 8pt; border-top-style: solid; border-right-style: solid; border-left-style: solid; border-bottom-style: solid; color: black;" AllowSorting="True"  >
                               <Columns>
                                   <asp:TemplateField HeaderText="Date Added" SortExpression="added7">
                                       <EditItemTemplate>
                                           <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("added7") %>'></asp:TextBox>
                                       </EditItemTemplate>
                                       <ItemTemplate>
                                           <br />
                                           <asp:Label ID="Label1" runat="server" Text='<%# Bind("added7") %>'></asp:Label><br />
                                           &nbsp;
                                       </ItemTemplate>
                                   </asp:TemplateField>
                                   <asp:BoundField DataField="ByWhom" HeaderText="Added By" SortExpression="ByWhom" />
                                   <asp:BoundField DataField="BOOKNO" HeaderText="Book #" SortExpression="BOOKNO" />
                                   <asp:BoundField DataField="ClearedBy" HeaderText="Cleared By" SortExpression="ClearedBy" Visible="False" />
                                   <asp:BoundField DataField="Done7" HeaderText="Date Done" SortExpression="Done7" Visible="False" />
                                   <asp:BoundField DataField="ForWhom" HeaderText="For Whom" SortExpression="ForWhom" />

我希望这最后一个字段显示为下面的行而不是列。

                                   <asp:TemplateField HeaderText="Note" SortExpression="Text">
                                       <EditItemTemplate>
                                           <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Text") %>'></asp:TextBox>
                                       </EditItemTemplate

                                       <ItemTemplate>
                                           <asp:Label ID="Label3" runat="server" Text='<%# Bind("Text") %>'></asp:Label>
                                       </ItemTemplate>
                                   </asp:TemplateField>
                               </Columns>
                           </asp:GridView>

2 个答案:

答案 0 :(得分:1)

尝试使用ListView而不是GridView,如果这是一个选项。这有许多模板功能。 模板希望:

<HeaderTemplate>
            <table>
        </HeaderTemplate>
        <ItemTemplate>

                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("col1") %>' ></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("col2") %>' ></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td colspan="10">
                        <asp:Label ID="Label3" runat="server" Text='<%# Eval("Text") %>' ></asp:Label>
                    </td>
                </tr>

        </ItemTemplate>
        <EditItemTemplate>

                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("col1") %>' ></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("col2") %>' ></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td colspan="10">
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Text") %>' ></asp:TextBox>
                    </td>
                </tr> 
        </EditItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>

答案 1 :(得分:1)

通过这种方式将所有行转换为列....

public DataTable ConvertColumnsAsRows(DataTable dt)
{
    DataTable dtnew = new DataTable();

    for (int i = 0; i <= dt.Rows.Count; i++)
    {
        dtnew.Columns.Add(Convert.ToString(i));
    }
    DataRow dr;

    for (int j = 0; j < dt.Columns.Count; j++)
    {
        dr = dtnew.NewRow();
        dr[0] = dt.Columns[j].ToString();
        for (int k = 1; k <= dt.Rows.Count; k++)
            dr[k] = dt.Rows[k - 1][j];
        dtnew.Rows.Add(dr);
    }
    return dtnew;
}

此函数用于将列转换为行