Hamamelis
Hamamelis

Reputation: 2115

Personalize GridView in C# Asp Net 4

This is my gridView populated from a database:

+---------+-------+----+---+
| Details | Combo | A  | S |
+---------+-------+----+---+
| View    | PCM   |  9 | 0 |
| View    | SAM   | 14 | 0 |
| View    | RAS   |  6 | 1 |
| View    | OUT   | 14 | 2 |
| View    | Tot   | 43 | 3 |
+---------+-------+----+---+

If click in the 'View' I have the details of row in another aspx page (GV.aspx).

As you can see in the last row I have provided the total of the columns.

I need to delete in the last line the 'View' in column 'Details' for this output:

+---------+-------+----+---+
| Details | Combo | A  | S |
+---------+-------+----+---+
| View    | PCM   |  9 | 0 |
| View    | SAM   | 14 | 0 |
| View    | RAS   |  6 | 1 |
| View    | OUT   | 14 | 2 |
|         | Tot   | 43 | 3 |
+---------+-------+----+---+

Can you help me? Thanks in advance.

This is the GV in my ASPX page:

        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            ShowHeader="true" CssClass="mGrid" EmptyDataText="No Data"
            DataKeyNames="Combo" Width="500">
            <AlternatingRowStyle CssClass="altrows" />
            <Columns>
                <asp:TemplateField HeaderText="Details">
                    <ItemTemplate>
                        <asp:HyperLink ID="Link" runat="server" NavigateUrl='<%# string.Format("~/GV.aspx?Combo={0}", HttpUtility.UrlEncode(Eval("Combo").ToString()))  %>'
                            Text="View">
                        </asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Combo" HeaderText="Combo" />
                <asp:BoundField DataField="A" HeaderText="A" />
                <asp:BoundField DataField="S" HeaderText="S" />
            </Columns>
        </asp:GridView>

Upvotes: 0

Views: 96

Answers (2)

SpiderCode
SpiderCode

Reputation: 10122

You can add Visible='<%#Eval("Combo").ToString() != "Total"%>' in your HyperLink as shown below:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
    ShowHeader="true" CssClass="mGrid" EmptyDataText="No Data"
    DataKeyNames="Combo" Width="500">
    <AlternatingRowStyle CssClass="altrows" />
    <Columns>
        <asp:TemplateField HeaderText="Details">
            <ItemTemplate>
                <asp:HyperLink ID="Link" runat="server" NavigateUrl='<%# string.Format("~/GV.aspx?Combo={0}", HttpUtility.UrlEncode(Eval("Combo").ToString()))  %>'
                    Text="View" Visible='<%#Eval("Combo").ToString() != "Total"%>'>
                </asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Upvotes: 1

j.f.
j.f.

Reputation: 3939

You could add the OnDataBound event to your GridView.

OnDataBound="GridView1_DataBound"

This will fire after the GridView has had all of its rows bound to it. Then in that handler, grab the last row and hide your control.

protected void GridView1_DataBound(object sender, EventArgs e)
{
    GridViewRow row = GridView1.Rows[GridView1.Rows.Count - 1];
    HyperLink Link = (HyperLink)row.FindControl("Link");
    Link.Visible = false;
}

Upvotes: 0

Related Questions