隐藏网格视图中最后一行的编辑和删除按钮
本文关键字:一行 编辑 按钮 删除 视图 网格 最后 隐藏 | 更新日期: 2023-09-27 17:56:04
我有一个网格视图,网格视图中的几列有金额计算。我在网格视图的最后一个添加了一个新的数据行,用于汇总列值并显示。
但是网格视图中的最后一行具有可见的编辑和删除按钮,我怎么可能从最后一行隐藏这两个图像?
<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
}
}