How to make certain rows read-only in DataGrid

Posted on 2007-10-01
Last Modified: 2008-01-09
If I have a DataTable with one of the columns serving as a flag that I will use to determine if a row should be editable or not, how can I check that flag and then based on that value, make each row either diabled (or just make it not editable would be fine also for each given row) if my flag meets a certain value for a given row.  I still want the use to be able to see that row.  

 I'm not sure where or how to do this and in what DataGrid Event...or if I should just loop through all rows in the Grid right after the line where I perform the DataBind?

I was thinking I could do something like this where cell 0 is my hidden flag field and cell 1 is the edit template cell or something.

    Protected Sub dgPhones_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles dgPhones.ItemDataBound

        If e.Item.Cells(0) = myvalue Then
            e.Item.Cells(1).Enabled = False
        End If
    End Sub
Question by:dba123
    LVL 1

    Expert Comment

    Try to do it on the RowEditing event, as the event argument allows you to cancel the edit

    LVL 1

    Expert Comment

    Oh, one more thing...

    From the RowEditing event arguments, there is a property called NewEditIndex

    So, you can do something like this to get access to your flag in the datasource


    If you use a datatable, you can cast this to a datarow and access the flag

    Ctype(GridView.Rows(e.NewEditIndex).DataItem, DataRow)("flag")

    Hope that this helps :)

    LVL 1

    Author Comment

    That's fine but it requires the user to actually click the edit button.  I don't want that edit button to show up or for it to be disabled so that they can't even click on it.  I want to disable that cell altogether or row altogether...either or.
    LVL 1

    Expert Comment

    if you want to delete the row, just implement a filter with a dataview and bind the gridview to the dataview.

    If you want to disable the button, try using javascript after the page is loaded
    LVL 1

    Accepted Solution

    figured it out.  You have to do it or can do it in the ItemDataBound.  At this point the items have already been bound to the grid.  I can then check whatever cell there and disable whatever other cell I want
    LVL 1

    Expert Comment

    Closed, 500 points refunded.
    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    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…
    This video discusses moving either the default database or any database to a new volume.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 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

    11 Experts available now in Live!

    Get 1:1 Help Now