How do you programmatically set a datagrid column to readOnly?

Posted on 2006-04-15
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?

Question by:bobsegrest
    LVL 24

    Expert Comment

    by:DBAduck - Ben Miller
    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?

    LVL 13

    Author Comment

    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"?

    LVL 3

    Expert Comment


    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,
    LVL 13

    Author Comment

    Can you show me how to do this in a Visual Basic (VB) environment?
    LVL 3

    Expert Comment

    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
     end sub

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

    LVL 13

    Author Comment

    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)

            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...

    LVL 13

    Author Comment

    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.


    Accepted Solution

    PAQed with points refunded (500)

    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now