Solved

Make web form datagrid column read only

Posted on 2003-12-06
2
366 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 - SQL Timeout 9 51
insert value of checklistbox checked 4 39
VB.NET 2008 Winforms Signing 13 42
C# Service FileSystemwatcher isse 9 47
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…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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