Programmatically access GridView columns and manipulate

后端 未结 2 949
我寻月下人不归
我寻月下人不归 2021-01-16 01:37

I have a GridView :



        
相关标签:
2条回答
  • 2021-01-16 02:04

    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.

    0 讨论(0)
  • 2021-01-16 02:24

    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.

    0 讨论(0)
提交回复
热议问题