How to enable column in GridView?

sams5817 picture sams5817 · Mar 26, 2013 · Viewed 9.4k times · Source

I've a ASP.NET GridView with the following data:

enter image description here

Rows will be disable OnRowDataBound based on the value on column3.

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;
        }
    }
}

however, I want column1 always enable, hyperlink in column1 should always clickable.

I've tried get the cells and enabled it, but it is not working.

kindly advise what is the workaround for the issue above.

Answer

Gajendra picture Gajendra · Mar 26, 2013

You can do this by enable/disable particular cell.

  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;

        }
    }
}