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

replacting GridViewDataColumn for GridViewComboBoxColumn

3 Answers 182 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Bart
Top achievements
Rank 1
Bart asked on 30 Nov 2016, 09:46 AM

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?

3 Answers, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 05 Dec 2016, 08:02 AM
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.
0
Bart
Top achievements
Rank 1
answered on 05 Dec 2016, 12:37 PM

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.
0
Martin
Telerik team
answered on 05 Dec 2016, 02:07 PM
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.
Tags
GridView
Asked by
Bart
Top achievements
Rank 1
Answers by
Martin
Telerik team
Bart
Top achievements
Rank 1
Share this question
or