Solved

Make web form datagrid column read only

Posted on 2003-12-06
2
361 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
Comment Utility
I believe you can use the DataGridColumnStyle.ReadOnly property.
0
 
LVL 7

Accepted Solution

by:
TransBind earned 100 total points
Comment Utility
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

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

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 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

12 Experts available now in Live!

Get 1:1 Help Now