如何在GridView中启用列?

时间:2013-03-26 17:32:53

标签: c# asp.net gridview rowdatabound

我有一个带有以下数据的ASP.NET GridView:

enter image description here

将根据column3上的值禁用OnRowDataBound。

GridView:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
        onrowdatabound="GridView1_RowDataBound1">
        <Columns>
            <asp:TemplateField HeaderText="Column1">
                <ItemTemplate>
                    <asp:HyperLink ID="hyperlink" runat="server" Text='<% #Eval("Dosage") %>' NavigateUrl='<% #Eval("Dosage") %>'></asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Column2">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<% #Eval("Drug") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Column3">
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<% #Eval("Patient") %>' ></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Column4">
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<% #Eval("Date") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

RowDataBound:

protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label a = e.Row.FindControl("Label3") as Label;
        if (a.Text == "Sam")
        {
            e.Row.Enabled = false;
            e.Row.Cells[0].Enabled = true;
        }
    }
}

但是,我希望column1始终启用,column1中的超链接应该始终可以点击。

我已尝试获取单元格并启用它,但它无效。

请告知上述问题的解决方法是什么。

1 个答案:

答案 0 :(得分:1)

您可以通过启用/禁用特定单元格来执行此操作。

  protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
  {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label a = e.Row.FindControl("Label3") as Label;
        if (a.Text == "Sam")
        {

            e.Row.Cells[0].Enabled = true;
            e.Row.Cells[1].Enabled = false;
            e.Row.Cells[2].Enabled = false;
            e.Row.Cells[3].Enabled = false;

        }
    }
}