tags:

views:

104

answers:

3

Hi,

I need to set the 'ReadOnly' property of a BoundField in a GridView to the value of a bit field in the recordset that is being displayed by the same GridView.

I am aware I could achieve this in code, but I was wondering, out of interest, if it's possible to do this declaratively inside the property using a <% %> snippet?

Cheers,

Jamie

A: 

If you need to bind to properties, I would use a TemplateField and then set the behavior there. This is only if you can't use the code, otherwise, that is your best bet.

I don't think there's a way to bind the detail of a BoundColumn directly.

Steve Wright
+1  A: 

Yes you can do this. Create a TemplatedField and in the binding statement use either

<%# ((Employee)Container.DataItem).IsApproved ? "yes" : "no" %>

or you can use a method from the code-behind

<%# FormatBool(((Employee)Container.DataItem).IsApproved) %>

where FormatBool is a property in your code-behind

protected string FormatBool(bool value)
{
   if (value)
      return "yes";
   return "no";
}
bendewey
A: 

I was unable to find a way of manipulating the properties of either the BoundField or the TemplateField declaratively based on the DetailView's data.

Bendewey very charitably assumed I was binding to a business object when in fact I am dealing with the data directly in this instance - apologies for not making that clear.

My solution in the code behind was the following:

    protected void DetailsView1_DataBound(object sender, EventArgs e)
    {
        DetailsView dv = (DetailsView)sender;

        if (dv.DataItemCount > 0)
        {
            DataRowView data = (DataRowView)dv.DataItem;
            bool isFixed = (bool)data["IsFixed"];

            if (isFixed)
            {
                dv.Rows[2].Enabled = false;
                dv.Rows[6].Enabled = false;
            }
        }
    }

I was disapointed that I had to reference the rows I wanted to disable by index. Possibly there's a way of referencing these rows by their data field without iterating through all the rows, but I couldn't find it at first glance.

Thanks for the help!

Jamie