Solved

Make web form datagrid column read only

Posted on 2003-12-06
2
364 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

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