C# 以编程方式访问GridView列并操作

C# 以编程方式访问GridView列并操作,c#,.net,asp.net,C#,.net,Asp.net,我有一个GridView: <asp:GridView ID="GridView1" runat="server" AllowPaging="True" GridLines="None" HorizontalAlign="Left" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" onrowcommand="GridView1_RowCommand1"&g

我有一个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>
我想查询数据库中特定字段的值,如果为真,则显示LinkButton。如果为false,我希望不显示linkButton

有没有办法以编程方式访问GridView,并使其某些列可见或操作其项


帮助。

您可以通过向事件添加处理程序来完成此操作。在代码隐藏中沿以下行添加事件处理程序:

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"];
    }
}
在标记中,将事件处理程序附加到网格:

<asp:GridView OnRowDataBound="myGrid_RowDataBound" ...>
您还需要为LinkButton分配一个id,该id与使用事件处理程序中的FindControl方法搜索的id相匹配


免责声明:我目前在Linux机器上,没有机会测试这个。请报告代码中的任何错误-如果您有编辑器权限,请随时更正它们。

您可以通过向事件添加处理程序来完成此操作。在代码隐藏中沿以下行添加事件处理程序:

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"];
    }
}
在标记中,将事件处理程序附加到网格:

<asp:GridView OnRowDataBound="myGrid_RowDataBound" ...>
您还需要为LinkButton分配一个id,该id与使用事件处理程序中的FindControl方法搜索的id相匹配

免责声明:我目前在Linux机器上,没有机会测试这个。请报告代码中的任何错误-如果您有编辑权限,请随时更正。

是的,有

1您需要订阅RowDataBound事件。 2给链接按钮一个ID。 3插入代码隐藏

  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如果这不适用于访问数据项,请开始考虑LinqDataSource。

是的

1您需要订阅RowDataBound事件。 2给链接按钮一个ID。 3插入代码隐藏

  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如果这不适用于访问数据项,请开始考虑LinqDataSource。

将不起作用,因为页眉和页脚调用相同的方法。插入类似于ife.Row.RowType==DataControlRowType.DataRowI个人更喜欢if数据!=空方法似乎很好。好吧,现在我明白你的解决方案了。我不会进一步讨论,因为你用了“个人偏好”这个词;由于页眉和页脚调用相同的方法,因此将不起作用。插入类似于ife.Row.RowType==DataControlRowType.DataRowI个人更喜欢if数据!=空方法似乎很好。好吧,现在我明白你的解决方案了。我不会进一步讨论,因为你用了“个人偏好”这个词;