How To Dynamically Disable Cells in the Grid?

14 posts, 1 answers
  1. Steve
    Steve avatar
    24 posts
    Member since:
    Mar 2010

    Posted 18 Jan 2011 Link to this post

    I have a grid that represents six months of data, where each cell represents a day.  The grid has 6 rows and 33 columns (Month, Days 1 through 31, Total).  How would I dynamically disable those cells that do not represent a valid date (e.g. Feb 30, Feb 31, Apr 31, etc.) so that the user cannot enter data in them?  The grid will either show the first half of a year, or the second half of a year (Jan - Jun, or Jul - Dec).
  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 19 Jan 2011 Link to this post

    Hi Steve Dentel,

    A possible approach may be to use the IsReadOnlyBinding that can be set to True for those cells you do not want to edit. You may take a look at our online documentation for a reference.
     

    All the best,
    Maya
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  3. DevCraft banner
  4. Steve
    Steve avatar
    24 posts
    Member since:
    Mar 2010

    Posted 19 Jan 2011 Link to this post

    I looked at the example (XAML).  How would I do this in the code behind?  What about the fact that I am trying to disable different cells (days), depending on the row (month).  Take a look at the screen shot showing my grid.  Each hand drawn red 'X' represents a date that I don't want to allow the user to edit.
  5. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 20 Jan 2011 Link to this post

    Hello Steve Dentel,

    You may define the IsReadOnlyBinding in the code-behind like follows:

    GridViewDataColumn column = new GridViewDataColumn();          
    column.DataMemberBinding = new Binding("Name");
    column.IsReadOnlyBinding = new Binding("IsActive");
    this.clubsGrid.Columns.Add(column);

    In this case IsActive is a property of the business object. You may take a look at this forum thread for a reference and a sample project demonstrating how you may use the IsReadOnlyBinding.
    Another possible approach may be to use a CellStyleSelector and color those cells which do not have any values for example. You may take a look at our online documentation and demos for further information.

    All the best,
    Maya
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  6. Steve
    Steve avatar
    24 posts
    Member since:
    Mar 2010

    Posted 20 Jan 2011 Link to this post

    Hello Maya,

    Can you make the TelerikSL2010Q2 solution available that was shown in the RadGridView - Style and Template Selectors video for Silverlight and WPF?

    Thanks,

    Steve

  7. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 20 Jan 2011 Link to this post

    Hi Steve Dentel,

    May you provide a bit more details which video are you referring to ? Do you require a sample demonstrating the usage of Style and Template Selectors ?
     

    All the best,
    Maya
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  8. Steve
    Steve avatar
    24 posts
    Member since:
    Mar 2010

    Posted 20 Jan 2011 Link to this post

    Hi Maya,

    It is the video on this page:  http://www.telerik.com/help/silverlight/gridview-cell-style-selector.html

    Thanks,

    Steve
  9. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 20 Jan 2011 Link to this post

    Hi Steve Dentel,

    You may find a sample project attached.

    All the best,
    Maya
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  10. Steve
    Steve avatar
    24 posts
    Member since:
    Mar 2010

    Posted 20 Jan 2011 Link to this post

    Hi Maya,

    That is not the solution displayed in the video...see attached screen shot.  A suggestion:  when training videos are published on your website that utilize a specific VS solution, please also publish the solutions shown in the video.

    Thank you.

    Steve
  11. Steve
    Steve avatar
    24 posts
    Member since:
    Mar 2010

    Posted 20 Jan 2011 Link to this post

    Hi Maya,

    Would you please provide the solution referenced in the video?

    Thanks,

    Steve
  12. Answer
    Maya
    Admin
    Maya avatar
    4062 posts

    Posted 24 Jan 2011 Link to this post

    Hello Steve Dentel,

    I am sending you the sample project used in the video.
    I hope that helps.
     

    Kind regards,
    Maya
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  13. Tyree
    Tyree avatar
    105 posts
    Member since:
    Dec 2006

    Posted 24 Jan 2011 Link to this post

    If you set the IsReadOnlyBinding={Binding DateString,Converter={StaticResource IsInvalidDate}}
    Where your DateString is not actually as obviously it would error, then in the converter you test if its a valid date...if not return true. Of course thats assuming your doing some sort of MVVM or add the property to your row item. If your trying to build a grid via the code behind you can do something more direct like {Binding Month /* 2/1/2011 */, Converter={StaticResource IsDateNotInMonth}, ConverterParamenter=29 /* day number */} checking if the parameter is valid for the month in question.
  14. Steve
    Steve avatar
    24 posts
    Member since:
    Mar 2010

    Posted 31 Jan 2011 Link to this post

    Thank you for providing the solution shown in the video.  After some trial and error, I am using the conditional style converter with three numeric converters.  Below is the xaml for the UserControl.Resources section

    <UserControl.Resources>
        <Style x:Key="Day29StyleIsValid"
               TargetType="rgv:GridViewCell">
            <Setter Property="IsEnabled" Value="True" />
        </Style>
        <Style x:Key="Day29StyleIsNotValid"
               TargetType="rgv:GridViewCell">
            <Setter Property="IsEnabled" Value="False" />
        </Style>
        <local:Day29Converter x:Key="zDay29Converter" />
        <local:ConditionalStyleSelector x:Key="Day29StyleSelector"
                    ConditionConverter="{StaticResource zDay29Converter}">
            <local:ConditionalStyleSelector.Rules>
                <local:ConditionalStyleRule Style="{StaticResource Day29StyleIsValid}">
                    <local:ConditionalStyleRule.Value>
                        <sys:Boolean>True</sys:Boolean>
                    </local:ConditionalStyleRule.Value>
                </local:ConditionalStyleRule>
                <local:ConditionalStyleRule Style="{StaticResource Day29StyleIsNotValid}">
                    <local:ConditionalStyleRule.Value>
                        <sys:Boolean>False</sys:Boolean>
                    </local:ConditionalStyleRule.Value>
                </local:ConditionalStyleRule>
            </local:ConditionalStyleSelector.Rules>
        </local:ConditionalStyleSelector>
        <Style x:Key="Day30StyleIsValid"
               TargetType="rgv:GridViewCell">
            <Setter Property="IsEnabled" Value="True" />
        </Style>
        <Style x:Key="Day30StyleIsNotValid"
               TargetType="rgv:GridViewCell">
            <Setter Property="IsEnabled" Value="False" />
        </Style>
        <local:Day30Converter x:Key="zDay30Converter" />
        <local:ConditionalStyleSelector x:Key="Day30StyleSelector"
                    ConditionConverter="{StaticResource zDay30Converter}">
            <local:ConditionalStyleSelector.Rules>
                <local:ConditionalStyleRule Style="{StaticResource Day30StyleIsValid}">
                    <local:ConditionalStyleRule.Value>
                        <sys:Boolean>True</sys:Boolean>
                    </local:ConditionalStyleRule.Value>
                </local:ConditionalStyleRule>
                <local:ConditionalStyleRule Style="{StaticResource Day30StyleIsNotValid}">
                    <local:ConditionalStyleRule.Value>
                        <sys:Boolean>False</sys:Boolean>
                    </local:ConditionalStyleRule.Value>
                </local:ConditionalStyleRule>
            </local:ConditionalStyleSelector.Rules>
        </local:ConditionalStyleSelector>
        <Style x:Key="Day31StyleIsValid"
               TargetType="rgv:GridViewCell">
            <Setter Property="IsEnabled" Value="True" />
        </Style>
        <Style x:Key="Day31StyleIsNotValid"
               TargetType="rgv:GridViewCell">
            <Setter Property="IsEnabled" Value="False" />
        </Style>
        <local:Day31Converter x:Key="zDay31Converter" />
        <local:ConditionalStyleSelector x:Key="Day31StyleSelector"
                    ConditionConverter="{StaticResource zDay31Converter}">
            <local:ConditionalStyleSelector.Rules>
                <local:ConditionalStyleRule Style="{StaticResource Day31StyleIsValid}">
                    <local:ConditionalStyleRule.Value>
                        <sys:Boolean>True</sys:Boolean>
                    </local:ConditionalStyleRule.Value>
                </local:ConditionalStyleRule>
                <local:ConditionalStyleRule Style="{StaticResource Day31StyleIsNotValid}">
                    <local:ConditionalStyleRule.Value>
                        <sys:Boolean>False</sys:Boolean>
                    </local:ConditionalStyleRule.Value>
                </local:ConditionalStyleRule>
            </local:ConditionalStyleSelector.Rules>
        </local:ConditionalStyleSelector>
    </UserControl.Resources>

    Here's the xaml on the grid for the three days that I want to check:

    <telerik:GridViewDataColumn x:Name="Day29Col" 
        DataMemberBinding="{Binding Path=Day29, Mode=TwoWay}" 
        Header="29" IsFilterable="False" IsSortable="False" 
        CellStyleSelector="{StaticResource Day29StyleSelector }" />
      
    <telerik:GridViewDataColumn x:Name="Day30Col" 
        DataMemberBinding="{Binding Path=Day30, Mode=TwoWay}" 
        Header="30" IsFilterable="False" IsSortable="False" 
        CellStyleSelector="{StaticResource Day30StyleSelector }" />
      
    <telerik:GridViewDataColumn x:Name="Day31Col" 
        DataMemberBinding="{Binding Path=Day31, Mode=TwoWay}" 
        Header="31" IsFilterable="False" IsSortable="False" 
        CellStyleSelector="{StaticResource Day31StyleSelector }" />

    And here is the first converter...the others are similar.  In the code below, ExerciseDate is always set to the first day of the month...e.g. 2/1/2011
    public class Day29Converter : IValueConverter
    {
        public object Convert(object value, 
            Type targetType, 
            object parameter, 
            System.Globalization.CultureInfo culture)
        {
            ParticipantExerciseDetail ped = value as ParticipantExerciseDetail;
            if (ped != null)
            {
                // Add 28 days to the ExerciseDate, which is always day 1.  
                // If the 29th is a valid date, then the month
                // component of day will not have changed.
                DateTime day = new DateTime();
                day = ped.ExerciseDate.AddDays(28);
                return day.Month == ped.ExerciseDate.Month ? true : false;
            }
            return null;
        }
        public object ConvertBack(object value, 
            Type targetType, 
            object parameter, 
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

  15. Jerry T.
    Jerry T. avatar
    118 posts
    Member since:
    Jul 2012

    Posted 01 Jul 2011 Link to this post

    How would one setup a CellStyleSelector using the MVVM method?

    I was looking at the attached project from the Posted on Jan 20, 2011 post and it looks like the general idea I need to use but we're using MVVM so I don't have direct access from the codebehind.

    Or, is there some way to access the grid's data thru the grid's properties/methods?

Back to Top
DevCraft banner