?
Solved

Make web form datagrid column read only

Posted on 2003-12-06
2
Medium Priority
?
368 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 400 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

752 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