将页脚行显示为gridview中的第一行

时间:2014-04-17 14:26:53

标签: c# asp.net .net gridview

我的gridview中有一个页脚模板,允许用户将新项添加到网格中。因为他们不希望每次都添加到页面下方,并且他们希望将该页脚行作为gridview的第一行。

ASPX:

 <asp:GridView ID="gvApplication" runat="server" AutoGenerateColumns="False" HorizontalAlign="center"
            ShowFooter="True" CellPadding="3" GridLines="Both" CssClass="contentfont" ShowHeaderWhenEmpty="True"
            EmptyDataText="No Records Found"  OnRowDataBound="gvApplication_RowDataBound"
            OnRowDeleting="gvApplication_RowDeleting" OnRowCommand="gvApplication_RowCommand"
            DataKeyNames="ID,Group,App_Name" Width="100%">
            <Columns>
                <asp:TemplateField HeaderText="ID"
                        <asp:Label ID="lblID" runat="server" Text='<%# Bind("[ID]") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="ddlID" runat="server" Width="150px" AutoPostBack="True"
                            OnSelectedIndexChanged="ddlID_SelectedIndexChanged" />
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Application Group">
                    <ItemTemplate>
                        <asp:Label ID="lblAppGrp" runat="server" Text='<%# Bind("[Group]") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="ddlGroup" runat="server" Width="150px" AutoPostBack="True"
                            OnSelectedIndexChanged="ddlGroup_SelectedIndexChanged" />
                    </FooterTemplate>
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Application Name">
                    <ItemTemplate>
                        <asp:Label ID="lblAppName" runat="server" Text='<%# Bind ("[App_Name]") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="ddlApp" runat="server" Width="150px" AutoPostBack="True"
                            OnSelectedIndexChanged="ddlApp_SelectedIndexChanged" />
                    </FooterTemplate>
                </asp:TemplateField>                    
                <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                        <asp:ImageButton ID="btnDelete" runat="server" CommandName="Delete" ImageUrl="~/Images/delete.gif"
                            ToolTip="Delete" OnClientClick="return confirm('Are you sure want to Delete');" />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:LinkButton ID="lnkAdd" Width="65px" runat="server" CausesValidation="False"
                            CommandName="AddNew" Text="Link Code" ForeColor="#3f6da2" Font-Bold="true">
                        </asp:LinkButton>
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
            <RowStyle Wrap="False" />
            <HeaderStyle Wrap="False" BackColor="#5B95CF" ForeColor="White" Height="25px" BorderStyle="Ridge" />
            <FooterStyle BackColor="#D1DDF1" ForeColor="White" Font-Bold="True" />
        </asp:GridView>

CS:

public void gvApplication_RowDataBound(object sender, GridViewRowEventArgs e)
{
    DataRowView drview = e.Row.DataItem as DataRowView;
    if (e.Row.RowType == DataControlRowType.Footer)
    {
        DropDownList ddlID = (DropDownList)e.Row.FindControl("ddlID");

        ddlID .DataSource = AppDataRepository.GetID();
        ddlID .DataValueField = "ID";
        ddlID .DataTextField = "ID";
        ddlID .DataBind();
        ddlID .Items.Insert(0, new ListItem("--Select ID--", "0"));
        ddlID .SelectedIndex = 0;

        DropDownList ddlGroup = (DropDownList)e.Row.FindControl("ddlGroup");
        ddlGroup.DataSource = AppDataRepository.GetGroup();
        ddlGroup.DataValueField = "Group";
        ddlGroup.DataTextField = "Group";
        ddlGroup.DataBind();
        ddlGroup.Items.Insert(0, new ListItem("--Select Group--", "0"));
        ddlGroup.SelectedIndex = 0;

        DropDownList ddlApp = (DropDownList)e.Row.FindControl("ddlApp");
        ddlApp.DataSource = AppDataRepository.GetApp();
        ddlApp.DataValueField = "App_Name";
        ddlApp.DataTextField = "App_Name";
        ddlApp.DataBind();
        ddlApp.Items.Insert(0, new ListItem("--Select App--", "0"));
        ddlApp.SelectedIndex = 0;
    }
}

尝试使用HeaderTemplate

<asp:TemplateField HeaderText="ID"
                    <asp:Label ID="lblID" runat="server" Text='<%# Bind("[ID]") %>'></asp:Label>
                </ItemTemplate>
                <HeaderTemplate>
                    <asp:DropDownList ID="ddlID" runat="server" Width="150px" AutoPostBack="True"
                        OnSelectedIndexChanged="ddlID_SelectedIndexChanged" />
                </HeaderTemplate>
            </asp:TemplateField>

2 个答案:

答案 0 :(得分:0)

FooterStyle-HorizontalAlign="Right"添加到模板的父列

答案 1 :(得分:0)

由于Internet Explorer放弃了对css top:expression的支持 - 我不得不将gridview标题分成一个单独的表。

我有一张桌子。在第一行/第一个单元格中,我放了另一个包含表头的表。 在下一行/第一个单元格中,我将div设置为比上表宽20px(允许div上的滚动条)。 div包含没有标题的gridview。在gridview的pre_render中,我在两个表中设置了列的宽度。

我花了一些时间检查用户屏幕的大小等,并设置tables / div的宽度(以及div的高度)。结果是一个带有静态标题的表,其宽度与下面的gridview完全相同。您可以轻松地执行此操作并在标头表中添加一个额外的行,以包含当前位于gridview的页脚行中的控件。