Programmatically access GridView columns and manipulate

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x