繁体   English   中英

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

[英]Hide edit & delete button for last row in gridview

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

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

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

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

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

请参阅: 在页脚中显示摘要数据

另外: 什么是XY问题?

示例(应该很容易转换为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

尝试这个

将编辑和删除按钮列的可见性设置为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
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM