views:

201

answers:

2

Hi,

I want to do something like this with a GridView:

<asp:CommandField ShowSelectButton="True" Visible='<%# return Eval("SC_TABLE") %>' />

But that doesn't work, coming up with error:

Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.CommandField does not have a DataBinding event.

Is there anyway I can set the visibility from the aspx page? PS: SC_TABLE exists from the datasource, so nothing wrong from that part.

+1  A: 

You could do this with a TemplateField instead...

<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton runat="server" ID=SelectButton CommandName="SELECT" Visible='<%# Eval("SC_TABLE") %>' Text="Select" />
    </ItemTemplate>
</asp:TemplateField>
Scott Ivey
+1: I was hoping to do it with CommandField. I already knew of this way, but its still help. Oh well, seems its impossible with CommandField. :(
waqasahmed
Unless someone else can provide a better answer, I will mark this answer as accepted in a afew days.
waqasahmed
+1  A: 

I found the answer at the end of this post:

Basically, you need to capture the RowCreated event on the DataGrid

OnRowCreated="GridView1_RowCreated"

Then, on the aspx.cs page use the following code to hide controls:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowIndex == 1)
    {
        e.Row.Cells[0].Controls.Clear();
    } 
}

It works if you have a CommandField in the first column.

Eric