隐藏编辑&删除gridview中最后一行的按钮

时间:2014-04-01 17:09:35

标签: c# asp.net .net gridview datarow

我有一个gridview,gridview中的几列有金额计算。我在gridview的最后一个添加了一个新的数据行,它总结了列值和显示。

但是gridview中的最后一行有可见的编辑和删除按钮,我怎么可能隐藏最后一行的那两个图像呢?

<asp:GridView ID="gvDetails" DataKeyNames="UserId,UserName" runat="server"
        AutoGenerateColumns="false" CssClass="Gridview" HeaderStyle-BackColor="#61A6F8"
        ShowFooter="true" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White"
        onrowcancelingedit="gvDetails_RowCancelingEdit"
        onrowdeleting="gvDetails_RowDeleting" onrowediting="gvDetails_RowEditing"
        onrowupdating="gvDetails_RowUpdating">
    <Columns>

        <asp:TemplateField>
            <EditItemTemplate>
                <asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Images/update.jpg" ToolTip="Update" Height="20px" Width="20px" />
                <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/Cancel.jpg" ToolTip="Cancel" Height="20px" Width="20px" />
            </EditItemTemplate>
            <ItemTemplate>
                <asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Images/Edit.jpg" ToolTip="Edit" Height="20px" Width="20px" />
                <asp:ImageButton ID="imgbtnDelete" CommandName="Delete" Text="Edit" runat="server" ImageUrl="~/Images/delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="ID">
            <EditItemTemplate>
                <asp:Label ID="lblID" runat="server" Text='<%#Eval("ID") %>'/>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblID" runat="server" Text='<%#Eval("ID") %>'/>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Profit">
            <EditItemTemplate>
                <asp:TextBox ID="txtProfit" runat="server" Text='<%#Eval("Profit") %>'/>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblProfit" runat="server" Text='<%#Eval("Profit") %>'/>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Amount">
            <EditItemTemplate>
                <asp:TextBox ID="txtAmount" runat="server" Text='<%#Eval("Amount") %>'/>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblAmount" runat="server" Text='<%#Eval("Amount") %>'/>
            </ItemTemplate>
        </asp:TemplateField>

    </Columns> 
</asp:GridView>

3 个答案:

答案 0 :(得分:2)

您可以以编程方式隐藏它们:

var lastRow = gvDetails.Rows[gvDetails.Rows.Count - 1];
lastRow.FindControl("imgbtnEdit").Visible = false;
lastRow.FindControl("imgbtnDelete").Visible = false;

理想情况下,这应该在网格视图是数据绑定并且包含所有行(包括最后一行)之后完成,但作为最后的手段,您可以使用Page_PreRender

答案 1 :(得分:1)

求和数据的最佳位置在页脚中,它将处理命令控件,因为它们没有添加到页脚。

请参阅:Displaying Summary Data in the Footer

另外:What is the XY Problem?

示例(应该很容易转换为C#):

'Note Global Declaration
Dim Total1 As Double = 0
Dim Total2 As Double = 0

Protected Sub GridView1_RowDataBound _
    (sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound    

    If e.Row.RowType = DataControlRowType.DataRow Then

        Dim drv as DataRowView = CType(e.Row.DataItem, DataRowView)

        ' Perform summations on Data rows
        Total1 += CDbl(drv("<column_name>").ToString())
        Total2 += CDbl(drv("<other_column_name>").ToString())

    ElseIf e.Row.RowType = DataControlRowType.Footer Then

        ' Place results in footer cells
        e.Row.Cells(2).Text = "Total: " & Total1.ToString()
        e.Row.Cells(3).Text = "Total: " & Total2.ToString()
    Endif
End Sub

答案 2 :(得分:0)

试试这个

设置编辑的可见性&amp;删除按钮列为false。您可以相应地设置列索引。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
        e.Row.Cells[0].Visible = false;   //0 is autogenerate edit column index
        e.Row.Cells[1].Visible = false;  // 1  is autogenerate delete column index
}
}