Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do you programmatically set a datagrid column to readOnly?

Posted on 2006-04-15
9
Medium Priority
?
554 Views
Last Modified: 2008-02-26
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
0
Comment
Question by:bobsegrest
8 Comments
 
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 16462103
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
 
LVL 13

Author Comment

by:bobsegrest
ID: 16462131
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
 
LVL 3

Expert Comment

by:meomar
ID: 16462477
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Author Comment

by:bobsegrest
ID: 16464004
Can you show me how to do this in a Visual Basic (VB) environment?
0
 
LVL 3

Expert Comment

by:meomar
ID: 16464060
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
 
LVL 13

Author Comment

by:bobsegrest
ID: 16464874
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
 
LVL 13

Author Comment

by:bobsegrest
ID: 16472503
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
 

Accepted Solution

by:
GranMod earned 0 total points
ID: 16676758
PAQed with points refunded (500)

GranMod
Community Support Moderator
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses
Course of the Month21 days, 5 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question