GridView标题没有与行排成一行

时间:2013-10-15 18:48:53

标签: c# visual-studio gridview

我有一个GridView,我将数据绑定到此网格没有任何问题。我遇到的问题是让标题与网格中的行匹配。

<asp:GridView ID="gvMyGrid" runat="server" 
            AutoGenerateColumns="False" BackColor="White" 
            BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
            ForeColor="Black" GridLines="Vertical">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <table>
                            <tr>
                                <td>
                                    Date    
                                </td>
                                <td>
                                    Merchant
                                </td>
                                <td>
                                    Description
                                </td>
                                <td>
                                    Amount
                                </td>
                                <td>
                                    Status
                                </td>
                            </tr>
                        </table>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <table>
                            <tr>
                                <td>
                                    <asp:Label ID="lblTransactionDate" runat="server" Text='<%# String.Format("{0:MM/dd/yyyy}", Convert.ToDateTime(Eval("transactionDate"))) %>'></asp:Label>            
                                </td>
                                <td>
                                    <asp:Label ID="lblAcceptLocation" runat="server" Text='<%# Eval("acceptLocation").ToString() %>'></asp:Label>            
                                </td>
                                <td>
                                    <asp:TextBox ID="tbItemDescription" runat="server"></asp:TextBox>            
                                </td>
                                <td>
                                    <asp:Label ID="lblAmount" runat="server" Text='<%# Eval("preAuthAmount") %>'></asp:Label>
                                </td>
                                <td>
                                    <asp:Label ID="lblStatus" runat="server" Text='<%# Eval("transactionStatus") %>'></asp:Label>            
                                </td>
                                <td>
                                    <asp:LinkButton ID="LinkButton1" runat="server" Text="Assign +" OnClick="BtnAssign"></asp:LinkButton>   
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#CCCC99" />
            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
            <RowStyle BackColor="#F7F7DE" />
            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FBFBF2" />
            <SortedAscendingHeaderStyle BackColor="#848384" />
            <SortedDescendingCellStyle BackColor="#EAEAD3" />
            <SortedDescendingHeaderStyle BackColor="#575357" />
        </asp:GridView>

标题的对齐全部被推到一起。我可以放置空格,但我宁愿让它看起来很漂亮,我也不确定我做错了什么。如果我使用Repeater,一切看起来都很棒,但对我来说这不是一个选择。我需要能够单击“分配”按钮并获取该行中单击的行和值。

1 个答案:

答案 0 :(得分:1)

你把你的网格弄错了。您在标题和每一行中定义了单独的表,更不用说将所有字段填充到一列中。

<asp:GridView ID="gvMyGrid" runat="server"
    AutoGenerateColumns="False" BackColor="White"
    BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
    ForeColor="Black" GridLines="Vertical">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:TemplateField>
            <HeaderTemplate>Date</HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblTransactionDate" runat="server" Text='<%# String.Format("{0:MM/dd/yyyy}", Convert.ToDateTime(Eval("transactionDate"))) %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>Merchant</HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblAcceptLocation" runat="server" Text='<%# Eval("acceptLocation").ToString() %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>Description</HeaderTemplate>
            <ItemTemplate>
                <asp:TextBox ID="tbItemDescription" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>Amount</HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblAmount" runat="server" Text='<%# Eval("preAuthAmount") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>Status</HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblStatus" runat="server" Text='<%# Eval("transactionStatus") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" Text="Assign +" OnClick="BtnAssign"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#CCCC99" />
    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
    <RowStyle BackColor="#F7F7DE" />
    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
    <sortedascendingcellstyle backcolor="#FBFBF2" />
    <sortedascendingheaderstyle backcolor="#848384" />
    <sorteddescendingcellstyle backcolor="#EAEAD3" />
    <sorteddescendingheaderstyle backcolor="#575357" />
</asp:GridView>