Solved

Make web form datagrid column read only

Posted on 2003-12-06
2
363 Views
Last Modified: 2012-06-27
I have done this before but I can't remember how you make columns read only.  

For example, my datagrid looks like this:

Name    ID     Time In     Time Out
joe        4      10:00 AM   11:00 PM
jane      5      9:30 AM     12:00 PM

When I go to the edit mode, I don't want the user to be able to edit the Name column or the ID column.  Thanks in advance
0
Comment
Question by:djhoen
2 Comments
 
LVL 2

Expert Comment

by:smitty22
ID: 9889175
I believe you can use the DataGridColumnStyle.ReadOnly property.
0
 
LVL 7

Accepted Solution

by:
TransBind earned 100 total points
ID: 9889332
The way to make a column read-only is to make the property you're binding to
read-only (i.e. remove its set accessor). However, this will cause problems
if the property is set when you populate the grid in the first place. One
effective way round this (though I'm not sure it's good practice) is to
increase the accessibility of the private field behind the property to
internal, so you can set this directly within your assembly.

The following example sets the DataGridColumnStyle object's ReadOnly property to the same value as the DataColumn object's ReadOnly property.

[Visual Basic]
Private Sub SetReadOnly()
    Dim myColumn As DataGridColumnStyle
    Dim myDataColumns As DataColumnCollection
    ' Get the columns for a table bound to a DataGrid.
    myDataColumns = dataSet1.Tables("Suppliers").Columns
    Dim dataColumn As DataColumn
    For Each dataColumn In myDataColumns
        dataGrid1.TableStyles(0).GridColumnStyles(dataColumn.ColumnName).ReadOnly = dataColumn.ReadOnly
    Next dataColumn
End Sub 'SetReadOnly

[C#]
private void SetReadOnly()
{
    DataColumnCollection myDataColumns;
    // Get the columns for a table bound to a DataGrid.
    myDataColumns = dataSet1.Tables["Suppliers"].Columns;
    foreach(DataColumn dataColumn in myDataColumns)
    {
        dataGrid1.TableStyles[0].GridColumnStyles[dataColumn.ColumnName].ReadOnly = dataColumn.ReadOnly;
    }
}

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

867 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

21 Experts available now in Live!

Get 1:1 Help Now