Mike
Mike

Reputation: 1067

How to hide a link in a cell of a gridview?

I have a gridview which displays a data from the database. In one of the tables in this database have a column to store details about attanchment file. If the attachment is available that column value will set as "YES". Otherwise it will set as "NO". What I want to do is, show a link to view the attachment. But if cell value of the column is "NO" (when there is no attachment) the link must be hidden.

Note : I know how to view a file. Here what Im expecting is to hide the link in the cell which doesn't have an attachment.

This is what I have done upto now.

if (ds.Tables[0].Rows.Count > 0)
{
     grdSo.DataSource = ds;
     grdSo.DataBind();
     for(int i=0; i <ds.Tables[0].Rows.Count; i++)
     {
          if (ds.Tables[0].Rows[i][6].Equals("NO"))
          {
               grdSo.Rows[i].Cells[6].Visible = false;
          }
         else
         {
               grdSo.Rows[i].Cells[6].Visible = true;
         }
     }
}       

I could hide the cell using this code. But unfortunately this hides the lines of the cell too. How can I avoid it happening?

Upvotes: 0

Views: 1775

Answers (3)

Manish Mistry
Manish Mistry

Reputation: 44

ASPX Code :

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
        <Columns>
            <asp:BoundField Visible="false" DataField="id" />
            <asp:TemplateField HeaderText="Has Attachment">
                <ItemTemplate>
                    <asp:Label ID="lblAtt" runat="server" Text='<%#Eval("HasAtt") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="View Attachment">
                <ItemTemplate>
                    <asp:LinkButton ID="lbtnAtt" runat="server" OnClick="lbtnAtt_Click" Visible="false">View</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

CS Code :

protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            this.BindGrid();
        }
    }

    private void BindGrid()
    {
        //Here you write databind logic
        // Datasource table of GridView1 should contain 'HasAtt' and 'id' as its binded to label.
    }

    private void ViewAttachment(int id)
    {
        //Here you write your logic to view attachment.
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow) // checking if row is datarow or not
        {
            Label lblHasAtt = e.Row.FindControl("lblAtt") as Label;
            LinkButton lbtnViewAtt = e.Row.FindControl("lbtnAtt") as LinkButton;
            lbtnViewAtt.Visible = (lblHasAtt.Text.ToLower() == "yes");
        }
    }

    protected void lbtnAtt_Click(object sender, EventArgs e)
    {
        LinkButton lbtnViewAtt = sender as LinkButton;
        GridViewRow grw = lbtnViewAtt.NamingContainer as GridViewRow;
        int id = Convert.ToInt32(this.GridView1.Rows[grw.RowIndex].Cells[0].Text); // retriving value of first column on 'lbtnAtt' click row.
        this.ViewAttachment(id);
    }

Upvotes: 1

Girish Vadhel
Girish Vadhel

Reputation: 745

One of the ways to do this is to use server side control like LinkButton to view the link that you want to show and set the visibility of the control as per your requirement in the OnRowDataBound event of the gridview.

Below is the code to show/hide LinkButton on OnRowDataBound event.

protected void gridId_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
          DataSourceClass varData = (DataSourceClass)e.Row.DataItem;
          // check if your data have flag to show the link
          if(varData.show)
            ((LinkButton)e.Row.FindControl("linkbuttonId")).Visible = true;
          else
            ((LinkButton)e.Row.FindControl("linkbuttonId")).Visible = false;
     }
}

Upvotes: 2

Brett Reinhard
Brett Reinhard

Reputation: 382

I recall being able to parse through the grid view by using a for each for the rows and using a for statement for columns. If I recall correctly, you can grab an item ie row.Item[i]

foreach(GridViewRow row in  GridView1.Rows)
{
    for(int i = 0; i < GridView1.Columns.Count; i++)
    {
        // here you can do the logic to decide whether to show or hide the text for this cell
    }
}

Sorry for formatting responding on my phone.

Upvotes: 1

Related Questions