Home All Groups Group Topic Archive Search About

Disable CommandField for some rows of data

Author
16 Mar 2006 9:08 PM
David Thielen
Hi;

Based on a column value in each row of data, I only want the CommandField to
be enabled if that column's value for that row is a specific value. For any
other value I want the control to still be there, but disabled.

How can I do that?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Author
16 Mar 2006 10:01 PM
Phillip Williams
Most likely you would have to use a TemplateField like I did with the
DetailsView:
http://www.webswapp.com/codesamples/aspnet20/detailsview/default.aspx

http://www.webswapp.com/codesamples/aspnet20/detailsview/default.aspx

                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="btnFax" runat="server" Text="Send
Fax" Enabled='<%# Eval("Fax").ToString().Trim<>String.Empty%>' />
                    </ItemTemplate>
                </asp:TemplateField>

The CommandField can render more than one button (select, delete, Insert)
none of which exposes the Enabled property.  You might use the Visible
property, e.g. e.Row.Cells[0].Controls[0].Visible= false, during the
RowDataBound event: http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.dataitem(VS.80).aspx

Show quoteHide quote
"David Thielen" wrote:

> Hi;
>
> Based on a column value in each row of data, I only want the CommandField to
> be enabled if that column's value for that row is a specific value. For any
> other value I want the control to still be there, but disabled.
>
> How can I do that?
>
> --
> thanks - dave
> david_at_windward_dot_net
> http://www.windwardreports.com
>
Author
17 Mar 2006 3:00 AM
Steven Cheng[MSFT]
Thanks for Phillip's good suggetion.

Hi Dave,

If you need complex customization, the "RowDataBound" will be the one you
need. Here is very simple use of it:

I just disable the command cell according to the RowIndex, you can do it
according to the DataBound value(get from e.Row.DataItem)


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs
e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if(e.Row.RowIndex == 3)
            e.Row.Cells[0].Enabled = false;
        }
    }

Hope this also helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)