Domanda

I have a gridview and few columns in gridview have amount calculations. And I have added a new datarow at the last of the gridview which sums up the column values and displays.

But the last row in gridview has edit and delete button visible and how could I possibly hide those two images from last row?

<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>
È stato utile?

Soluzione

You can hide them programatically:

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

Ideally this should be done after grid view is data bound and has all rows (including the last one), but as a last resort you can use Page_PreRender.

Altri suggerimenti

The Best place for summation data is in the footer, that will take care of having the command controls as they are not added to the footer.

See: Displaying Summary Data in the Footer

Also: What is the XY Problem?

Example (should be easy to convert to 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

Try this

set visibility of your edit & delete button column to false. You can set the column index accordingly.

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
}
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top