Set GridViewComboBoxColumn Value Programmatically

2 posts, 0 answers
  1. Stephen
    Stephen avatar
    43 posts
    Member since:
    Apr 2012

    Posted 03 Dec 2012 Link to this post

    I am having a bear of a time trying to set the value of a GridViewComboBoxColumn via code.  I have the BeginningEdit event, which I use to grab the value of the cell when editing starts.  Then I check to see if the cell value is null or empty in the CellValidating event.  I can detect that it is wrong.  That part works fine.  I cannot figure out how to set it to the previous value, if their entered value isn't valid.

    (My end goal is that when the user clears the value and leaves the field, it replaces the null entry with the value the cell had when they entered it.)

    Here is my GridViewComboBoxColumn code:

    <telerik:RadGridView x:Name="rgvCorrectionDetails"
                            Width="1036"
                            MaxWidth="1036"
                            Margin="5"
                            HorizontalAlignment="Left"
                            AutoGenerateColumns="False"
                            CanUserDeleteRows="False"
                            CanUserInsertRows="False"
                            IsReadOnly="True"
                            ScrollViewer.VerticalScrollBarVisibility="Auto"
                            BeginningEdit="rgvCorrectionDetails_BeginningEdit"
                            SelectionMode="Extended"
                            CellValidating="rgvCorrectionDetails_CellValidating">
        <telerik:RadGridView.Columns>
            <telerik:GridViewComboBoxColumn UniqueName="rcbxGridHoursType"
                                            DataMemberBinding="{Binding HoursTypeCode}"
                                            DisplayMemberPath="DropDownEntry"
                                            EditTriggers="CellClick"
                                            Header="Hours Type"
                                            IsComboBoxEditable="True"
                                            IsReadOnlyBinding="{Binding IsRowReadOnly}"
                                            SelectedValueMemberPath="EntryStringId"
                                            TextSearch.TextPath="DropDownEntry"
                                            EditorStyle="{Binding Source={StaticResource RadComboBoxStandard}}" />
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>


    Here is my BeginningEdit event:

    private void rgvCorrectionDetails_BeginningEdit(object sender, GridViewBeginningEditRoutedEventArgs e)
    {
        var _Row = rgvCorrectionDetails.SelectedItem as HoursCorrectionDetail;
     
        if (_Row != null)
        {
            if (_Row.IsRowReadOnly)
            {
                e.Cancel = true;
            }
            else
            {
                 // Row can be edited, so save off previous value
                CurrentHoursTypeCode = _Row.HoursTypeCode;
                CurrentHoursType = _Row.HoursType;
            }
        }
    }


    Here is my CellValidating event (you can see a few of my attempts to get the right thing):

    private void rgvCorrectionDetails_CellValidating(object sender, GridViewCellValidatingEventArgs e)
    {
      if (e.Cell.Column.UniqueName == "rcbxGridHoursType")
      {
         if (JobCodes.Any(pEntry => pEntry.DropDownEntry == (String) e.NewValue))
         {
            // This is good
         }
         else
         {
            // This is bad, need to reset value
            // ((GridViewComboBoxColumn) rgvCorrectionDetails.Columns["rcbxGridHoursType"]).SelectedValueMemberPath = CurrentHoursTypeCode;
            // e.Cell.Value = CurrentHoursType;
         }
       }
    }


  2. Yoan
    Admin
    Yoan avatar
    1215 posts

    Posted 06 Dec 2012 Link to this post

    Hello Stephen,

     In order to achieve your main goal, your object needs to implement IEditableObject interface. You can check this MSDN article for a reference. I can suggest you to check our blog post, where you can find information about how to implement this.

    All the best,
    Yoan
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top