How to get Context within DevExpress Column

39 Views Asked by At

I have the following code snippet:

<DxGrid ...>
    <Columns>
        <DxGridDataColumn ...>
            <CellDisplayTemplate>
                @{
                    var t = GetData((MyType)context.DataItem);
                }
                <DxComboBox ...
                            Value="@t"
                            NullText="Select Member" />
             </CellDisplayTemplate>
        </DxGridDataColumn>
    </Columns>
</DxGrid>

This works. However, I would like to move the inline code within Columns, so that I can access @t from another DxGridDataColumn. So essentially, I want this:

<DxGrid ...>
    <Columns>
       @{
           var t = GetData((MyType)context.DataItem);
        }
        <DxGridDataColumn Caption="Allocated Crew Member" MinWidth="80">
            <CellDisplayTemplate>
                <DxComboBox ...
                            Value="@t"
                            NullText="Select Member" />
             </CellDisplayTemplate>
        </DxGridDataColumn>
    </Columns>
</DxGrid>

The problem with the above is that Context does not exist for anything outside CellDisplayTemplate. How can I get Context from within Columns?

1

There are 1 best solutions below

0
On

Maybe it is not the real solution for your problem, but i have some advice.

The Context Variable allows u to define a template using one of the Contexts entrys. Your Context gets defined by your Data Source. So you won't get values of other rows while defining those templates.

You propably set the context/source like this:

<DxGrid Data=""YourSourceData>
    <Columns>
    ...
    </Columns>
</DxGrid>

and to KEEP IT SIMPLE u should not try overly fancy stuff inside those columns trying to get values of other columns.

What I suggest is, you do the math in you corresponding *.razor.cs File and simply set YourDataSource with it. When u need to transform your values to other types, you could think about making a Viewmodel for it. So you transform your model to a Viewmodel and you set YourSourceData = new List<YourViewModel>;

This way you also seperate concerns, meaning you define your UI in your *.razor and your logic in you *.razor.cs .