添加空行时删除Gridview中的命令按钮

时间:2014-05-01 20:29:19

标签: c# gridview

当数据行为空时,我在合并包含gridview的“编辑”和“删除”按钮的列时遇到问题。
当dataTable中没有数据时,我向dataTable添加一个空行,以便显示包含添加按钮的页脚。 如果我不尝试合并空行中的列,那很好。但是当这行为空或者至少禁用它们时,我想删除'edit'并删除'按钮。

当用户从下拉列表中选择不包含任何部分的课程时,这是代码:

 protected void ddlCourses_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Get CourseId
        int tiCourseID = Convert.ToInt32(ddlCourses.SelectedValue.ToString());

        SectionGridView.DataSource = CSectionInfo.getDataUsingCourseID(tiCourseID);
        SectionGridView.DataBind();

        if (CSectionInfo.CurrentSectionCount == 0)
        {
            SectionGridView.Rows[0].Cells[0].ColumnSpan = SectionGridView.Columns.Count;        
            SectionGridView.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;        
            SectionGridView.Rows[0].Cells[0].Text = "No Section Data was found for this Course";
        }
    }

这是包含“编辑和删除按钮:

的网格视图
 <asp:Content runat="server" ID="SectionContent" ContentPlaceHolderID="SectionContent">
<section class="section">
<div class="content-wrapper">
<asp:Label ID="lblSelectCourse" runat="server" Text="Select Course: " CssClass="label"></asp:Label>
<asp:DropDownList ID="ddlCourses" runat="server" AutoPostBack="true" Height="25px" Width="180px" OnSelectedIndexChanged="ddlCourses_SelectedIndexChanged"></asp:DropDownList>
<br />
<br />
<asp:GridView ID="SectionGridView" runat="server" AllowSorting="True" AutoGenerateColumns="False" Caption="Section Information" CaptionAlign="Top" 
    CssClass="grid" HorizontalAlign="Left" ShowFooter="True" AllowPaging="True" PageSize="4" ShowHeaderWhenEmpty="true" 
    OnPageIndexChanging="SectionGridView_PageIndexChanging">
    <Columns>
         <asp:TemplateField HeaderText="SectionID" SortExpression="cgvSectionID">
            <ItemTemplate>
                <asp:Label ID="sgvLblSectionID" runat="server" Text='<%# Bind("SectionID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="CourseID" SortExpression="sgvCourseID">
            <EditItemTemplate>
                <asp:DropDownList ID="sgvDdlCourseID" runat="server" Text='<%# Bind("CourseID") %>'></asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="sgvLblCourseID" runat="server" Text='<%# Bind("CourseID") %>'></asp:Label>
            </ItemTemplate>
             <FooterTemplate>
                <asp:DropDownList ID="sgvInsertCourseID" runat="server" Width="90%"></asp:DropDownList>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name" SortExpression="sgvName">
            <EditItemTemplate>
                <asp:TextBox ID="sgvEditName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="sgvLblName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="sgvInsertName" runat="server" Width="90%"></asp:TextBox>
            </FooterTemplate>
            <HeaderStyle ForeColor="Black" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="SKU" SortExpression="sgvSKU">
            <EditItemTemplate>
                <asp:TextBox ID="sgvEditSKU" runat="server" Text='<%# Bind("SKU") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="sgvLblSKU" runat="server" Text='<%# Bind("SKU") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="sgvInsertSKU" runat="server" Width="90%"></asp:TextBox>
            </FooterTemplate>
            <HeaderStyle ForeColor="Black" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Registration Open Date" SortExpression="sgvRegOpenDate">
            <EditItemTemplate>
                <asp:TextBox ID="sgvEditRegOpenDate" runat="server" Text='<%# Bind("RegistrationOpen", "{0:MM-dd-yyyy}") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="sgvLblRegOpenDate" runat="server" Text='<%# Bind("RegistrationOpen", "{0:MM-dd-yyyy}") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="sgvInsertRegOpenDate" runat="server" Width="90%"></asp:TextBox>
            </FooterTemplate>
            <HeaderStyle ForeColor="Black" />
        </asp:TemplateField>

         <asp:TemplateField HeaderText="Registration Close Date" SortExpression="sgvRegCloseDate">
            <EditItemTemplate>
                <asp:TextBox ID="sgvEditRegCloseDate" runat="server" Text='<%# Bind("RegistrationClose", "{0:MM-dd-yyyy}") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="sgvLblRegCloseDate" runat="server" Text='<%# Bind("RegistrationClose", "{0:MM-dd-yyyy}") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="sgvInsertRegCloseDate" runat="server" Width="90%"></asp:TextBox>
            </FooterTemplate>
            <HeaderStyle ForeColor="Black" />
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Section Start Date" SortExpression="sgvSecStartDate">
            <EditItemTemplate>
                <asp:TextBox ID="sgvEditSecStartDate" runat="server" Text='<%# Bind("SectionStart", "{0:MM-dd-yyyy}") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="sgvLblSecStartDate" runat="server" Text='<%# Bind("SectionStart", "{0:MM-dd-yyyy}") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="sgvInsertSecStartDate" runat="server" Width="90%"></asp:TextBox>
            </FooterTemplate>
            <HeaderStyle ForeColor="Black" />
        </asp:TemplateField>

         <asp:TemplateField HeaderText="Section End Date" SortExpression="sgvSecEndDate">
            <EditItemTemplate>
                <asp:TextBox ID="sgvEditSecEndDate" runat="server" Text='<%# Bind("SectionEnd", "{0:MM-dd-yyyy}") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="sgvLblSecEndDate" runat="server" Text='<%# Bind("SectionEnd", "{0:MM-dd-yyyy}") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="sgvInsertSecEndDate" runat="server" Width="90%"></asp:TextBox>
            </FooterTemplate>
            <HeaderStyle ForeColor="Black" />
        </asp:TemplateField>

         <asp:TemplateField HeaderText="Member Price" SortExpression="sgvMemberPrice">
            <EditItemTemplate>
                <asp:TextBox ID="sgvEditMemberPrice" runat="server" Text='<%# Bind("MemberPrice", "{0:C}") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="sgvLblMemberPrice" runat="server" Text='<%# Bind("MemberPrice", "{0:C}") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="sgvInsertMemberPrice" runat="server" Width="90%"></asp:TextBox>
            </FooterTemplate>
            <HeaderStyle ForeColor="Black" />
        </asp:TemplateField>

         <asp:TemplateField HeaderText="Non-member Price" SortExpression="sgvNonMemberPrice">
            <EditItemTemplate>
                <asp:TextBox ID="sgvEditNonMemberPrice" runat="server" Text='<%# Bind("NonMemberPrice", "{0:C}") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="sgvLblNonMemberPrice" runat="server" Text='<%# Bind("NonMemberPrice", "{0:C}") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="sgvInsertNonMemberPrice" runat="server" Width="90%"></asp:TextBox>
            </FooterTemplate>
            <HeaderStyle ForeColor="Black" />
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Action" ShowHeader="False">
            <EditItemTemplate>
                <asp:LinkButton ID="sgvImgUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
                &nbsp;<asp:LinkButton ID="sgvImgCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:LinkButton ID="sgvEditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
                <asp:LinkButton ID="sgvDeleteButton" runat="server" CausesValidation="false" CommandName="Delete" Text="Delete"></asp:LinkButton>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
            <FooterTemplate>
                <asp:Button ID="sgvBtnAdd" runat="server" CommandName="Add" Text="Add Section" Width="90%" />
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>

</asp:GridView>

如果数据行为空,如何删除删除和编辑链接按钮?

谢谢,

1 个答案:

答案 0 :(得分:0)

我决定使用'EmptyDataTemplate'添加一个合并的列行,其中包含文本'此部分没有数据',第二行包含页脚模板中的所有控件以允许插入数据。