none
How can I make a column in a datagrid read-only

    Question

  • Andy Keung's series on the CourseManager app shows how to make individual controls read-only.  I'd like to make a column in a datagrid read-only and force users to use a command button to update.  That way I can enforce some validation rules on the set of detail records.  The goal is to have only 1 of the detail rows set as the default value.

    Alternatively, is there a way to programmatically catch a change to one row so that, if the user checks one row as the default, I can uncheck all the others?

    Tuesday, September 11, 2012 10:52 PM

Answers

  • Hi,

    You can do it as the code below:

            partial void EditableCustomersGrid_InitializeDataWorkspace(List<IDataService> saveChangesTo)
            {
                this.FindControl("grid").ControlAvailable += (s, e) =>
                    {
                        var grid = e.Control as DataGrid;
                        grid.Columns[0].IsReadOnly = true;
                    };
            }

    Hope helpful.
    • Proposed as answer by babloo1436 Thursday, September 13, 2012 3:31 AM
    • Marked as answer by Duke Carey Thursday, September 13, 2012 12:48 PM
    • Unmarked as answer by Duke Carey Thursday, September 13, 2012 10:01 PM
    • Marked as answer by Otomii Lu Monday, September 17, 2012 3:06 AM
    Thursday, September 13, 2012 2:25 AM
  • 4.Click Properites in the right view and give a name to the DataGrid:

    5.Then Click Write Code in the Designer and choose EditableCustomersGrid_InitializeDataWorkspace.

    6.Add the code to the EditableCustomersGrid_InitializeDataWorkspace method:

        public partial class EditableCustomersGrid
        {
            partial void EditableCustomersGrid_InitializeDataWorkspace(List<IDataService> saveChangesTo)
            {
                this.FindControl("grid").ControlAvailable += (s, e) =>
                    {
                        var grid = e.Control as DataGrid;
                        grid.Columns[0].IsReadOnly = true;
                    };
            }
        }

    7.Run the application and you can find that the first column have been locked.
    • Marked as answer by Duke Carey Saturday, September 15, 2012 12:14 AM
    Friday, September 14, 2012 1:46 AM
  • Hi,

    Please follow my steps below:

    1.Create a Table named Customer with two columns: LastName and FirstName.

    2.Create a EditableCustomersGrid for the Table:

    3.Click the DataGrid in the Designer:

    • Marked as answer by Otomii Lu Monday, September 17, 2012 3:06 AM
    Friday, September 14, 2012 1:37 AM

All replies

  • Hi,

    You can do it as the code below:

            partial void EditableCustomersGrid_InitializeDataWorkspace(List<IDataService> saveChangesTo)
            {
                this.FindControl("grid").ControlAvailable += (s, e) =>
                    {
                        var grid = e.Control as DataGrid;
                        grid.Columns[0].IsReadOnly = true;
                    };
            }

    Hope helpful.
    • Proposed as answer by babloo1436 Thursday, September 13, 2012 3:31 AM
    • Marked as answer by Duke Carey Thursday, September 13, 2012 12:48 PM
    • Unmarked as answer by Duke Carey Thursday, September 13, 2012 10:01 PM
    • Marked as answer by Otomii Lu Monday, September 17, 2012 3:06 AM
    Thursday, September 13, 2012 2:25 AM
  • You could also just change the type of control for the grid's column to its read-only equivalent, the same as for non-grid controls.

    TextBox --> Label

    DatePicker --> DateViewer

    etc


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.

    Thursday, September 13, 2012 3:37 AM
  • Yann -

    Where I'm working today I don't have Lightswitch available and can't access it until tomorrow.  However, I do have a couple of noob questions in the meanwhile -

    First question: I don't recall stumbling across any options to change the controls in the datagrid.  Where should I be looking?

    Second question: The control in question is a checkbox.  Is there a read-only equivalent?

    Thanks for your help.

    Thursday, September 13, 2012 10:22 AM
  • Otomii  - Thanks for the code.  I can't try it until tomorrow - don't have access to Lightswitch where I'm working today - but will definitely give it a go tomorrow
    • Edited by Duke Carey Thursday, September 13, 2012 12:46 PM
    Thursday, September 13, 2012 10:23 AM
  • It's OK. "noob" questions are always welcome.

    1. You can make the entire row use read-only controls by ticking the "Use Read-Only Controls" checkbox for the DataGridRow itself (which is only doing the manual steps for you). You can change the type of control that's used to present a property, by clicking on the small dropdown arrow for each control. A list of the available controls for the selected property's type appears, & you select the control you want from the list. The dropdown is not hugely intuitive, until you "know" that its there.

    2. For a CheckBox, you would need to use the programmatic method that Otomii suggested, unless you choose a Label as the control (from memory, by default this would give you True/False text). You could change that by defining "choices" for the boolean property in the table designer.


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.

    Thursday, September 13, 2012 12:14 PM
  • Thanks Yann.  I'll pore over the datagrid's options and the grid's embedded control options tomorrow.  I like the visual presentation of a checkbox control, so will likely stick with it and use the code suggested by Otomii.  Really glad to know about how I can change other column's controls to read-only analogs of the editable controls, though, so thanks for that valuable info
    • Edited by Duke Carey Thursday, September 13, 2012 12:48 PM
    Thursday, September 13, 2012 12:46 PM
  • Otomii -

    I'm back in front of my LS app now and cannot figure out where your code would go.  The screen is named ContactDetails.  Here's an image of the screen designer:

    Under Write Code I cannot figure out where to put your code.  Can you provide some guidance?  I have a Primary property in each of the grids, and each will have to be read-only.

    Thursday, September 13, 2012 10:11 PM
  • Hi,

    Please follow my steps below:

    1.Create a Table named Customer with two columns: LastName and FirstName.

    2.Create a EditableCustomersGrid for the Table:

    3.Click the DataGrid in the Designer:

    • Marked as answer by Otomii Lu Monday, September 17, 2012 3:06 AM
    Friday, September 14, 2012 1:37 AM
  • 4.Click Properites in the right view and give a name to the DataGrid:

    5.Then Click Write Code in the Designer and choose EditableCustomersGrid_InitializeDataWorkspace.

    6.Add the code to the EditableCustomersGrid_InitializeDataWorkspace method:

        public partial class EditableCustomersGrid
        {
            partial void EditableCustomersGrid_InitializeDataWorkspace(List<IDataService> saveChangesTo)
            {
                this.FindControl("grid").ControlAvailable += (s, e) =>
                    {
                        var grid = e.Control as DataGrid;
                        grid.Columns[0].IsReadOnly = true;
                    };
            }
        }

    7.Run the application and you can find that the first column have been locked.
    • Marked as answer by Duke Carey Saturday, September 15, 2012 12:14 AM
    Friday, September 14, 2012 1:46 AM
  • Suggestion... use it on the Cretaed or Activated, otherwise you will get random "Screen has not loaded yet" error messages

    SebaXOR

    Sunday, June 16, 2013 6:44 AM
  • Good suggestion Seb. But only in Created, not Activated. Any code in Activated will load any time that the screen gets focus.

    So code that only needs to be run once should go in Created.


    Yann Duran
         - Co-Author of Pro Visual Studio LightSwitch 2011
         - Author of the  LightSwitch Central Blog

    FREE Download: Luminous Tools for LightSwitch
    (a Visual Studio productivity extension for LightSwitch)
     
    Click Mark as Answer, if someone's reply answers your question
    Click  Vote as Helpful, if someone's reply is helpful
     
    By doing this you'll help everyone find answers faster.

    Monday, June 17, 2013 4:38 AM