skip to Main Content

if I want to supply a calculated value to a component such as a grid or any parameter really, and if there is no value then I want to leave the value as it was since it is being automatically calculated by that component, such as a default col width.

Basically I want to say parameter = calculated value or if not found leave as it was, i’m struggling with having to provide a value in the ‘or’ case that is affecting the original defaulted value…like this:

Width="@dict.GetValueOrDefault(colWidth, "")"

If I provide the "" as the default then it overrides what the component was doing on its own.

Alternatively:

Width = valueFound ? value : 0;

The zero is now overriding the original value, and I don’t have access to ‘Width’ since it is a parameter of a grid component, not something I can access if that makes sense.

I want to do this:

Width = valueFound ? value : "LEAVE WHATEVER WIDTH WAS ORIGINALLY";

TYIA

Actual example

<DxGrid Data="@data"
    @ref="Grid">
<Columns>
    @foreach (DataColumn col in data.Columns)
    {
        <DxGridDataColumn FieldName="@data.colName"
                          Width="@valueFound ? value : **LEAVE WHATEVER WIDTH WAS ORIGINALLY**
        </DxGridDataColumn>                                
     }
</Columns>

2

Answers


  1. What if u do like this?

    <DxGrid Data="@data"      @ref="Grid">
      <Columns>
        @foreach (DataColumn col in data.Columns)
        {
          @if (valueFound)
              {
               <DxGridDataColumn FieldName="@name"  Width="@value"> </DxGridDataColumn>                                
              }
              else
              {
                <DxGridDataColumn FieldName="@name"> </DxGridDataColumn>                                
              }
         }
       </Columns>
    
    Login or Signup to reply.
  2. You’re fighting the Razor syntax. The other answer provided works for a single parameter, but if you have more than one it starts to get very messy.

    You can revert to RenderTreeBuilder code which is what the Razor compiler actually produces.

    @foreach (DataColumn col in data.Columns)
    {
        __builder.OpenComponent<DxGridDataColumn>(0);
        __builder.AddComponentParameter(1, "FieldName", data.colName);
        if (data.width is not null)
            __builder.AddComponentParameter(2, "Width", data.width);
    
        __builder.CloseComponent();
    }
    

    Or build a method;

        private RenderFragment<DataColumn> BuildGridColumn => col => builder =>
        {
            builder.OpenComponent<DxGridDataColumn>(0);
            builder.AddComponentParameter(1, "FieldName", data.colName);
            if (data.width is not null)
                builder.AddComponentParameter(2, "Width", data.width);
    
            builder.CloseComponent();
        };
    

    and then call it;

    @foreach (DataColumn col in data.Columns)
    {
        @BuildGridColumn(col)
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search