This is a migrated thread and some comments may be shown as answers.

Set GridViewComboBoxColumn Value Programmatically

1 Answer 141 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Stephen
Top achievements
Rank 1
Stephen asked on 03 Dec 2012, 06:43 PM
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;
     }
   }
}


1 Answer, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 06 Dec 2012, 05:14 PM
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.

Tags
GridView
Asked by
Stephen
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Share this question
or