tags:

views:

89

answers:

2

I have a GridView :

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" GridLines="None" 
                HorizontalAlign="Left" AutoGenerateColumns="False" 
                DataSourceID="SqlDataSource1" onrowcommand="GridView1_RowCommand1">            
                <HeaderStyle HorizontalAlign="Left" />                            
                <Columns>  
                   <asp:TemplateField HeaderStyle-Width="150">
                        <HeaderTemplate>
                            <b>Downloads</b>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <!-- <asp:HyperLink ID="hyperlinkDownload" runat="server" NavigateUrl="" >Download 
                            MP3</asp:HyperLink> -->
                            <asp:LinkButton CommandName="download"
                             CommandArgument='<%# Eval("Name") %>' runat="server">Download MP3</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>    

</asp:GridView>

I want to query the value of a particular field in a DB and if it's true, display the LinkButton. if false, I want the linkButton not to be displayed.

is there a way to access the GridView programmatically and make visible certain of its columns or manipulate its items ?

help.

+2  A: 

You can do this by adding a handler to the RowDataBound event. Add an event handler along this lines of this in your code behind:

protected void myGrid_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    var data = e.Row.DataItem as DataRowView;
    if (data != null)
    {
        var lbtDownload = e.Row.FindControl("lbtDownload");
        lbtDownload.Visible = (bool) data.Row["HasFileForDownload"];
    }
}

In your markup, attach the event handler to the grid:

<asp:GridView OnRowDataBound="myGrid_RowDataBound" ...>

You will also need to assign an id to the LinkButton, matching the one that you are search for using the FindControl() method in the event handler.

Disclaimer: I am on currently a Linux machine with no chance of testing this. Please report any bugs in the code - feel free to correct them if you have editor rights.

Jørn Schou-Rode
Will not work, due to Header and Footer call the same method. Insert something like if(e.Row.RowType == DataControlRowType.DataRow)
citronas
I personally prefer the `if (data != null)` approach which seems to work just fine.
Jørn Schou-Rode
Well ok, now I do understand your solution. I won't discuss it further, because you used the term 'personal preference' ;)
citronas
+2  A: 

Yes there is.

1) You need to subscribe the RowDataBound event.
2) Give the LinkButton an ID.
3) Insert in codebehind

  protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
  {
    if(e.Row.RowType == DataControlRowType.DataRow)
    { 
      LinkButton _bt = e.Row.FindControl("ID") as LinkButton;
      if(_bt != null)
      {
        // have a look at the e.row.DataItem and try to get the value of your desired visibility property
        _bt.Visible = true;
      }
    }
  }

4) If this does not work with accessing the DataItem, start thinking about a LinqDataSource.

citronas