1

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.

AndreyAkinshin
  • 18,603
  • 29
  • 96
  • 155
sams5817
  • 1,037
  • 10
  • 34
  • 49
  • 1
    Enabled or Disable doesn't really matter for **Label** control. Currently, `e.Row.Enabled = false;` is useless in your code. – Win Mar 26 '13 at 17:55
  • My Column 1 is HyperLink instead of Label, Disable of row cause the HyperLink not clickable – sams5817 Mar 27 '13 at 03:43

1 Answers1

2

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;

        }
    }
}
Gajendra
  • 1,291
  • 1
  • 19
  • 32