How do you programmatically set a datagrid column to readOnly?

Hello Everyone,

I am working on an ASP.Net web page using VB in a Visual Studio 2003 development environment.

I have a datagrid displayed on my page that dynamically integrates data from two databases.

When a row is selected for editing, I need to programmatically set which columns are (or are not)  read-only based on the data already contained in the column.  

My project specification does not allowed me to hide any of the columns in my datagrid while editing.  What I am looking for is the programmatic equivalent to checking the ReadOnly check box for the column.

Can anyone tell me how to do this?

Bob
LVL 13
bobsegrestAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DBAduck - Ben MillerPrincipal ConsultantCommented:
You could either put the EditItemTemplate in the markup of the datagrid, or you can programmatically change the column in edit mode in the ItemCreated event of the DataGrid.

Are you putting the data in a TextBox?

Ben.
0
bobsegrestAuthor Commented:
Hi Ben,

When the client clicks the edit link for a row in the datagrid, I need to programmatically decide which columns are to be edited and which are not.  By default, all of the columns are editable and thus are displayed as text boxes.  If I set a column to ReadOnly in the designer, the column is displayed as a label rather than a textbox.  What I am seeking is a way to make this selection dynamically at run time.

Can you provide me with a code example of how I "can programmatically change the column in edit mode in the ItemCreated event of the DataGrid"?

Bob
0
meomarCommented:
Hi,

What you need to do is to handle this logic in the datagrid editcommand event that fires when the user selects a row and thw way of doing this is as follows:


private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
            { TextBox box = new TextBox();
                                   // By default edit item in the datgrid will be textbox always so you will need
                                  // To cast to a textbox and then based on your calcaulation set it to readonly or whatever
                                 // the index here is the column index of the datagrid  
                  box =    (TextBox) e.Item.Cells[0];
                                              if( certain logic that you need to handle)
                                               {
                                                  box.ReadOnly = true;
                                                }
            }

good luck,
meomar
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

bobsegrestAuthor Commented:
Can you show me how to do this in a Visual Basic (VB) environment?
0
meomarCommented:
Sub ItemsGrid_Edit(sender As Object, e As DataGridCommandEventArgs)

         Dim qtyText As TextBox = CType(e.Item.Cells(3).Controls(0), TextBox)
         Dim priceText As TextBox = CType(e.Item.Cells(4).Controls(0), TextBox)
 
         Dim item As String = e.Item.Cells(3).Text
         Dim qty As String = qtyText.Text
         if  qty = "1"
       
          priceText.ReadOnly= true
         endif
 end sub


I am sorry I do not write in VB, but it is technicly the same concepts  

       
     
0
bobsegrestAuthor Commented:
OK, here is what I tried,

    Private Sub dgRescPlans_EditCommand(ByVal source As Object, _
                                        ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
                                        Handles dgRescPlans.EditCommand

        ' Selecting the Edit button causes a postback, so we need to read the dataset we
        '   saved as a session variable during the Page_Load setup and bind it to the datagrid
        dsRescPlans = CType(Session("dsRescPlans"), DataSet)
        dgRescPlans.DataBind()

        Dim FullOS As TextBox = CType(e.Item.Cells(0).Controls(1), TextBox)
        FullOS.ReadOnly = True

    End Sub

When the 'Dim FullOS' line executed I received a 'Specified cast is not valid. ' error.

For some reason I don't believe the 'e' argument returned with this event includes the selected datarow.  It does include an index to the selected datarow, and one normally uses this to set the editItemIndex in the datagrid.  For example:

    dgRescPlans.EditItemIndex = e.Item.ItemIndex

However at the instant in time when this event is called, the selected line in the datagrid is not yet displayed in 'edit' mode and as such the textbox that you are suggesting I set to read only does not exist.

Clearly one of us is missing something here.  Frankly, I'll be happy for it to be me.  Please enlighten me...

Bob
0
bobsegrestAuthor Commented:
OK, Here is what I found...

The best place to implement this change is in the dataGrid_ItemDataBound event.  This event fires off one time for each row in your grid After the databind and before it is rendered.  if e.Item.ItemType = ListItemType.EditItem you are looking at the row selected for editing and you can easily clear (delete) the existing textbox control and add a new control of any type you choose.  In my case I used a label control.

If you simply set the original textbox control to read only, the grid will render a dysfunctional textbox.

I hope this helps the next person with this issue....

Please refund my points.

Bob
0
GranModCommented:
PAQed with points refunded (500)

GranMod
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.