CSLADataSource, GridView and editing

CSLADataSource, GridView and editing

Old forum URL: forums.lhotka.net/forums/t/4990.aspx


uwillmore posted on Wednesday, June 18, 2008

Hi everyone,

I know this is more of an ASP.NET or C# question than an CSLA question, but I am not finding the answer anywhere else, so I thought you might be able to help me out. So here goes:

I have a CSLADataSource that is assigned to a GridView. The Grid is set to autogenerate Edit, Update and Delete buttons and the CSLADataSource has event handlers assigned for each of the 3 event initiated by the buttons when one is clicked. I load the GridViewwith data in the OnSelectedObject event handler using a CSLA BusinessBaseList object that is initialized from the database. Everything so far is straight forward and causes no problems. But now I got to do something a little different. I have one item in each row that may be modified only if another item in the same row has a specific value, well actually does not have a specific value. Essentially I need to set the ReadOnly, Visible, or Enable property for a specific cell depending on the value of another cell. I can do this in on-edit mode just fine using code similar to this.

If (GridView1.rows[currentRow].Cells[3].text.ToString() == "AllowEdit")

{GridView1.rows[CurrentRow].Cells[5].Enabled = true;

GridView1.rows[CurrentRow].Cells[5].Visible= true;}

else

{GridView1.rows[CurrentRow].Cells[5].Enabled = false;

GridView1.rows[CurrentRow].Cells[5].Visible= false;}

This works fine and does what I want, until I click the Edit button. When the Edit button is clicked the grid is redisplayed with only one row and now all columns/fields in the row are visible and editable, regardless of the values of the ReadOnly, Visible, or Enable properties. The only property that seems to affect what can be edited is the ReadOnly Property. If I set ReadOnly to True when I declare the column ( i.e. in the HTML or in the ColumnProperties pop-up in VisualStudio) then the field can not be changed in edit mode. Unfortunately, ReadOnly is not a property of the Cells collection and so I can not set its value at run time.

And here is the question that all this leads to:

Does anyone know how to reset the Readonly property of a cell in a GridView at run-time?

or

Does anyone know how to make a specific field in a row non-editable? I can use the RowEditing event handler to do this, which gives me the  index of the row being edited in  the GridViewEditEventArgs parameter, if that helps.

Any help would be greatly appreciated.

TIA

URW

uwillmore replied on Sunday, June 22, 2008

I found the solution to this problem. It is really simple too.

I needed to set the Enabled property in the RowDataBound event not in the RowEditing event handler. That's it.

I thought you might like to know

URW

Copyright (c) Marimer LLC