locked
How do I set a checkbox to be readonly in a datagrid RRS feed

  • Question

  • I have a datagrid with a column checkboxes.  How do I make some of the checkboxes readonly?  Is there a way to also "gray out" the read only checkboxes?

     xam

     

     <Grid x:Name="LayoutRoot" Background="White" >        
            <sdk:DataGrid Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Top" x:Name="StudentGrid" Width="400" Height="255"
               HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" Margin="0,0,-2,0" >
            </sdk:DataGrid>        
     </Grid>
     l

     

     

    c#

     StudentGrid.ItemsSource = new StudentData().GetStudents();

     

    C# (StudentData object)

     public class StudentData
        {
            public List<Student> GetStudents()
            {
                List<Student> studentlist = new List<Student>();
                studentlist.Add(new Student() {Option=false, ID = 101, FirstName = "First", LastName = "Last", SSN="1234", DOB = new DateTime(1975, 2, 23), Gender = "Male", Classification = "Senior" });
                studentlist.Add(new Student() {Option=false, ID = 102, FirstName = "First1", LastName = "Last1", SSN="1224", DOB = new DateTime(1979, 4, 13), Gender = "Female", Classification = "Freshman" });
                studentlist.Add(new Student() {Option=false, ID = 103, FirstName = "First2", LastName = "Last2", SSN="1134", DOB = new DateTime(1985, 3, 23), Gender = "Male", Classification = "Junior" });
                return studentlist;
            }
    }
     
    Thursday, July 1, 2010 9:43 AM

Answers

  • You could specify the columns in the datagrid manually (turn auto generate off), and set the enabled property of the checkbox you specify to false.

    Thursday, July 1, 2010 9:46 AM
  • you will need to set autogeneratecolumns to false and define your columns then

    have a look over here for details on the formatConverter

     http://dotnetslackers.com/Community/blogs/bmdayal/archive/2009/04/28/silverlight-2-0-datagrid-demo-application-with-formatting-of-data.aspx

    then using the same concept  with isEnabled

    IsEnabled="{Binding Path=VALUEtoLookAt, converter={StaticResource SecurityConverter_IsEnabled}, ConverterParameter=999}"

    The variable you specify on the path= is passed into the converter as value, converterParameter is passed in as parameter and is optional.

    If you want the entire object passed in

    IsEnabled="{Binding converter={StaticResource SecurityConverter_IsEnabled}}" 
     

    In the converter return true or false

     

     

     

     

    Thursday, July 1, 2010 9:10 PM
  •  Yes you can either bind the enabled property of the checkbox to a value, set the object, or the item in the collection thats bound to the check box in code behind. You couldm try to capture the loadingRow event of the datagrid and update the value there for the checkbox

    Friday, July 2, 2010 8:28 AM

All replies

  • one way would be to set autogeneratecolumns to false and then define the columns in the grid

    some examples here

    http://blogs.msdn.com/b/scmorris/archive/2008/03/27/defining-columns-for-a-silverlight-datagrid.aspx

    Thursday, July 1, 2010 9:46 AM
  • You could specify the columns in the datagrid manually (turn auto generate off), and set the enabled property of the checkbox you specify to false.

    Thursday, July 1, 2010 9:46 AM
  • Hi,

    You can do this using a DataTemplate.

    HTH. Mark as answered if that helps :-)

    Regards,

    Vidds

    Thursday, July 1, 2010 9:49 AM
  • I do not want to make the entire column of checkboxes read only.  At run time, I need to make a few checkbox cells readonly.  And therefore I am thinking that I need to loop through the datagrid. or datagrid itemsource.  Can this be done at runtime?  I should be able to do this.

    Thursday, July 1, 2010 8:27 PM
  • you will need to set autogeneratecolumns to false and define your columns then

    have a look over here for details on the formatConverter

     http://dotnetslackers.com/Community/blogs/bmdayal/archive/2009/04/28/silverlight-2-0-datagrid-demo-application-with-formatting-of-data.aspx

    then using the same concept  with isEnabled

    IsEnabled="{Binding Path=VALUEtoLookAt, converter={StaticResource SecurityConverter_IsEnabled}, ConverterParameter=999}"

    The variable you specify on the path= is passed into the converter as value, converterParameter is passed in as parameter and is optional.

    If you want the entire object passed in

    IsEnabled="{Binding converter={StaticResource SecurityConverter_IsEnabled}}" 
     

    In the converter return true or false

     

     

     

     

    Thursday, July 1, 2010 9:10 PM
  •  Yes you can either bind the enabled property of the checkbox to a value, set the object, or the item in the collection thats bound to the check box in code behind. You couldm try to capture the loadingRow event of the datagrid and update the value there for the checkbox

    Friday, July 2, 2010 8:28 AM
  • Thanks for the help.  That works.  I used the a datatemplate with a checkbox inside.  the checkbox was bound to objects.  When the enabled property was false, the checkbox fuctioned as a readonly checkbox.

    Saturday, July 3, 2010 5:52 PM