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

Autogenerated radgridview with some fields binded to existing columns

2 Answers 105 Views
GridView
This is a migrated thread and some comments may be shown as answers.
François
Top achievements
Rank 1
François asked on 08 Feb 2018, 03:29 PM

Greeting,

I am currently working on an MVVM application using a Radgridview to present information to the user. The data I have to present is in the form of a table (2D array) that I load from .CSV file.  Since the number of column changes depending on the loaded file, I use a DataTable and then bind it to my RadGridView with AutoGeneratingColumn. Up to there everything works fine.  The problem I have is that there is 2 columns in the DataTable that are always present and that I want to bind to a preexisting column inside the grid.  As you can see in the following code snippet, my 2 columns are called IsUsed (a boolean) and Status (also a bool)

<telerik:GridViewCheckBoxColumn
                        Name="colTest"
                        AutoSelectOnEdit="True"
                        DataMemberBinding="{Binding IsUsed, Mode=TwoWay}"
                        EditTriggers="CellClick">
                        <telerik:GridViewCheckBoxColumn.EditorStyle>
                            <Style BasedOn="{StaticResource GridViewCheckBoxStyle}" TargetType="telerik:GridViewCheckBox">
                                <Setter Property="Background" Value="{Binding Status, Converter={StaticResource BooltoColorConverter}}" />
                            </Style>
                        </telerik:GridViewCheckBoxColumn.EditorStyle>
                    </telerik:GridViewCheckBoxColumn>
 

 

Currently the columns are auto generated and the predefined binding does not work. Is there a way to do this binding? I tried intercepting the AutoGenerating event but I don't know what to do from there.

 

Thank you and have a great day!

2 Answers, 1 is accepted

Sort by
0
Maurice
Top achievements
Rank 1
answered on 12 Feb 2018, 04:22 AM

The only way I have been successful with this is to write some custom code in the form of attributes and behaviors and then use the attribute information per object property (column) to build the column I need. There is an example here - https://www.telerik.com/forums/data-annotations-6b1f0642a51b#CmMncZAEeUevRVHxionr-g - using this approach I have full control over the grid columns, their style, editors, column types without losing things like dependency injection, note I have moved away from the linked POC code using a service locator pattern to an abstract factory pattern that is bound to the behavior after being injected in to the ViewModel. Also with this approach, it is all or nothing based on whether or not the grid is autogenerating columns.

 

Hope that helps,
Maurice

0
François
Top achievements
Rank 1
answered on 20 Feb 2018, 03:25 PM

Sorry for the long hiatus. I got caught in another project.

 

Thank you for your answer Maurice. It is a really interesting approach but it is not exactly what I want. My goal is not to add attributes to an auto-generated column, but to retrieve the bindings from an auto-generated column and to copy them into a predefined column. Basically my problem is that I need to combine the information from 2 different columns that I know will be there (just a quick reminder the rest of the DataTable is generated from a file and can vary a lot), one to the property IsChecked, and the other to IsEnable.

 

Currently I can access the DataMemberBinding during the creation of the column and copy it into my predefined column.  It doesn't generate errors when I create the table, but the path is wrong and in the GUI I see this message : BindingExpression path error:'IsUsed' property not found on 'object' "DataRow' (HashCode=36653303)'.

I am adding a bit of code to help describe what I desire. Here is the code behind that doesn't work :

private void DataGrid_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
{
    if ((string)e.Column.Header == "IsUsed")
    {
        colTest.DataMemberBinding = ((GridViewBoundColumnBase)e.Column).DataMemberBinding;
        e.Cancel = true;
    }
}

 

And here is the XAML (slightly different from before) :

<telerik:RadGridView
    Margin="0,0,0,0"
    AutoGenerateColumns="True"
    AutoGeneratingColumn="DataGrid_AutoGeneratingColumn"
    CanUserInsertRows="False"
    CanUserSelect="True"
    IsFilteringAllowed="False"
    ItemsSource="{Binding StandardConcentration, Mode=TwoWay}"
    LeftFrozenColumnCount="1"
    RowIndicatorVisibility="Collapsed"
    ShowGroupPanel="False">
    <telerik:RadGridView.Columns>
        <telerik:GridViewCheckBoxColumn
            Name="colTest"
            AutoSelectOnEdit="True"
            DataMemberBinding="{Binding IsUsed, Mode=TwoWay}"
            EditTriggers="CellClick">
        </telerik:GridViewCheckBoxColumn>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

 

I hope this gives a better description of my problem.

 

Thank you and have a great day!

 

Tags
GridView
Asked by
François
Top achievements
Rank 1
Answers by
Maurice
Top achievements
Rank 1
François
Top achievements
Rank 1
Share this question
or