replacting GridViewDataColumn for GridViewComboBoxColumn

4 posts, 0 answers
  1. Bart
    Bart avatar
    20 posts
    Member since:
    May 2014

    Posted 30 Nov 2016 Link to this post

    Hi,

    At the moment I'm facing an issue, I have a RadGridView which can show different tables with different definitions(not at the same time)
    So AutoGenerateColumns is set to True.

    now it can happen that some columns have an FK to another table.
    Before I replaced those columns in the DataLoaded event but because of performance reasons I would like to do those replaces in the CurrentCellChanged event.

    The problem when I do this is that the new GridViewComboBoxColumn misses the link to the actual datasource, and while reading the value for saving in the database, the value is DBNULL.

    the gridview in de Xaml looks like this

    <DataTemplate x:Key="AdminViewMainSectorTemplate">
            <Grid Margin="10">
                <Grid>
                    <telerik:RadBusyIndicator x:Name="BusyIndicator" Grid.Row="2" >
                        <telerik:RadGridView x:Name="gridViewAdminData"
                                                         ItemsSource="{Binding DataRecords, Mode=TwoWay}"
                                             RowEditEnded="EditRecord"
                                             Deleted="deleteRecord"
                                             AddingNewDataItem="AddingNewDataItem"
                                             MinColumnWidth="100"
                                             ColumnWidth="*"
                                             NewRowPosition="Top"                                         
                                             AutoGeneratingColumn="AutoGeneratingColumn"
                                             DataLoading="LoadData"
                                             DataLoaded="DataLoaded"
                                             CurrentCellChanged="CurrentCellChanged"
                                             SelectionUnit="Cell"
                                             >
                            <!--DataLoading="LoadData"
                                             DataLoaded="DataLoaded"-->
                            <telerik:EventToCommandBehavior.EventBindings>
                                <telerik:EventBinding
                                Command="{Binding HandleAddingNewRecordToDevicesGridCommand}"
                                EventName="AddingNewDataItem"
                                PassEventArgsToCommand="True"/>
                            </telerik:EventToCommandBehavior.EventBindings>
                        </telerik:RadGridView>
                    </telerik:RadBusyIndicator>
                </Grid>
            </Grid>
        </DataTemplate>

    and my code behind looks like this:

    private void CurrentCellChanged(object sender, GridViewCurrentCellChangedEventArgs e)
           {
               RadGridView rgv = (RadGridView)sender;
               GridViewCell cell = rgv.CurrentCell;
               if (cell != null && typeof(GridViewDataColumn) == cell.Column.GetType())
               {
                   Models.MyEntities db = new Models.MyEntities();
                   string tableName = Helpers.GlobalParameters.currenTableName;
                   if (tableName =="batches")
                   {
                       string fieldname = cell.Column.UniqueName;
                       if (fieldname == "ProductieOrderId")
                       {
                           int index = cell.Column.DisplayIndex;
                           GridViewComboBoxColumn cbb = new GridViewComboBoxColumn();
                           cbb.DataMemberBinding = new System.Windows.Data.Binding(fieldname);
                           //cbb.DataMemberBinding = cell.DataColumn.DataMemberBinding;
                           cbb.ItemsSource = db.productieorder.ToList();
                           cbb.SelectedValueMemberPath = fieldname;
                           cbb.DisplayMemberPath = "Id";
                           //cbb.Name = fieldname;
                           cbb.UniqueName = fieldname;
                           cbb.DisplayIndex = index;
                           rgv.Columns.Remove(cell.Column);
                           //rgv.Columns.Remove(cell.DataColumn);
                           rgv.Columns.Add(cbb);
     
                       }
                   } else if (tableName =="artikel")

    where the Field "Id" is the related field on the related table
    If I do this
    cbb.SelectedValueMemberPath = "Id";

    what I would say is logical, the shown value in the combobox stays empty.

    does anyone know where to look to fix this issue?

  2. Martin
    Admin
    Martin avatar
    160 posts

    Posted 05 Dec 2016 Link to this post

    Hi Bart,

    I have tested the scenario you described and everything works as expected. I am sending you the sample project I used for reproducing the case. Please take a look at it and let me know if it differs from your setup.


    Regards,
    Martin Vatev
    Telerik by Progress
    Telerik UI for WPF is ready for Visual Studio 2017 RC! Learn more.
  3. Bart
    Bart avatar
    20 posts
    Member since:
    May 2014

    Posted 05 Dec 2016 Link to this post

    Hi Martin,

    thank you for your answer, your solution didn't work out of the box for me because I was using some dynamic proxies, but it gave me some ideas to start with.

    I made an eventhander which updated the specific record. then I did soms more research and found out the only thing I actually needed to do was remove this row.

    cbb.DataMemberBinding = new System.Windows.Data.Binding(fieldname);
    I'm not sure why, but it works.

    Thank you.
  4. Martin
    Admin
    Martin avatar
    160 posts

    Posted 05 Dec 2016 Link to this post

    Hi Bart,

    I am really glad to see that you were able to resolve the issue and the attached example was helpful.

    Regards,
    Martin Vatev
    Telerik by Progress
    Telerik UI for WPF is ready for Visual Studio 2017 RC! Learn more.
Back to Top