0

我有一个 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>

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

4

1 回答 1

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>
于 2013-10-15T19:01:22.613 回答