<GridColumn Field="CustomerID" HeaderText="Customer Name" Width="150">
<Template Context="myContext">
@{
var af = (TItem)myContext;
System.Reflection.PropertyInfo pi = af.GetType().GetProperty("CustomerID");
<Syncfusion.Blazor.Buttons.SfCheckBox CssClass="e-outline e-success"
Checked=@((bool)(pi.GetValue(af, null)))> </Syncfusion.Blazor.Buttons.SfCheckBox>
}
</Template>
</GridColumn> |
<SfGrid DataSource="@Orders" AllowPaging="true">
<GridPageSettings PageSize="5"></GridPageSettings>
<GridColumns>
<GridColumn Field="OrderID" HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120"></GridColumn>
<GridColumn Field="CustomerID" HeaderText="Customer Name" Width="150">
<Template Context="myContext">
@{
var af = (TItem)myContext;
bool val = GetValue(af);
<SfCheckBox CssClass="e-outline e-success" @bind-Checked=@(val) TChecked="bool"> </SfCheckBox>
}
</Template>
</GridColumn>
<GridColumn Field="OrderDate" HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn>
<GridColumn Field="Freight" HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn>
</GridColumns>
</SfGrid>
@code{
[Parameter]
public List<TItem> Orders { get; set; }
public bool GetValue(TItem col)
{
System.Reflection.PropertyInfo pi = col.GetType().GetProperty("CustomerID");
return (bool)pi.GetValue(col, null);
}
}
|
<SfGrid @ref="Grid" DataSource="@Orders" AllowPaging="true">
<GridPageSettings PageSize="5"></GridPageSettings>
<GridColumns>
<GridColumn Field="OrderID" HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120"></GridColumn>
<GridColumn Field="CustomerID" HeaderText="Customer Name" Width="150">
<Template Context="myContext">
@{
var af = (TItem)myContext;
bool val = GetValue(af);
<SfCheckBox CssClass="e-outline e-success" @bind-Checked=@val ValueChange="@((ChangeEventArgs<bool> args) => valueChange(args.Checked,af))"></SfCheckBox>
}
</Template>
</GridColumn>
<GridColumn Field="OrderDate" HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn>
<GridColumn Field="Freight" HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn>
</GridColumns>
</SfGrid>
@code{
[Parameter]
public List<TItem> Orders { get; set; }
SfGrid<TItem> Grid { get; set; }
public bool GetValue(TItem col)
{
System.Reflection.PropertyInfo pi = col.GetType().GetProperty("CustomerID");
return (bool)pi.GetValue(col, null);
}
public void valueChange(bool va,TItem value)
{
var idProp = typeof(TItem).GetProperty("OrderID");
if (idProp != null)
{
object id = value.GetType().GetProperty("OrderID").GetValue(value, null);
TItem result = Orders.Where(x => idProp.GetValue(x).Equals(id)).FirstOrDefault();
result.GetType().GetProperty("CustomerID").SetValue(result, va);
}
} |
Hi!
I decided to write in this thread because i have similar issue.
I want to wrap Syncfusion components in my own Base components so the external dependencies are abstracted away in these base components.
I have Class Person that has property named Country which is of type Enum. I want this property be displayed as DropDownList in Grid.
I have created component called GridDropDownColumnBase. Column gets displayed as expected, but value of this column can't be changed without error.
As you can see in below image Template renders nicely, but when value is changed then error occurs, that is displayed in Console.
I have added simple demo project that displays vanilla Syncfusion grid and wrapped grid in base components that throws this error.
Am i doing something wrong or this can't be achieved?
<GridBase TValue="Person" Toolbar="Toolbaritems" DataSource=@Persons>
<GridColumnBase Field="Id" IsPrimaryKey="true" AllowAdding="false" AllowEditing="false" HeaderText="Id"></GridColumnBase>
. . .
<EditTemplate>
<SfDropDownList ID="Country" FloatLabelType="Syncfusion.Blazor.Inputs.FloatLabelType.Always"
TValue="Countries" TItem="string"
DataSource=@CountriesList @bind-Value=@((context as Person).Country)>
</SfDropDownList>
</EditTemplate>
</GridDropDownColumnBase>
</GridBase> |
[GridDropDownColumnBase.razor]
@using Syncfusion.Blazor.Grids
@using static TItemDemo.Pages.Index
@inherits GridColumn
<CascadingValue Value="@this">
@ChildContent
</CascadingValue>
|
[GridColumnBase.razor]
@using Syncfusion.Blazor.Grids
@using static TItemDemo.Pages.Index
@inherits GridColumn
<CascadingValue Value="@this">
@ChildContent
</CascadingValue> |
Hi Rahul,
Thank you for solution, tested it and it works. Didn't thought about inheritance and going that way.
Best regards,
Mairis
Hi Rahul,
Question about SfDropDownList if i wanted to wrap it also. How
I tried this:
@typeparam TValue
@typeparam TItem
@inherits SfDropDownList<TValue, TItem>
<CascadingValue Value="@this">
@ChildContent
</CascadingValue>
But it doesn't work.
[DdlComp.razor]
@using Syncfusion.Blazor.DropDowns
@using System.Linq.Expressions;
@using static TItemDemo.Pages.Index
@typeparam TVal;
@typeparam TItemss;
<SfDropDownList Value="@Value" ValueChanged="ValueChanged" @oninput="@InputHandler" ValueExpression="@ValueExpression" TValue="TVal" TItem="TItemss" ShowClearButton="true" Placeholder="e.g. Australia" DataSource="@Country">
<DropDownListFieldSettings Text="Name" Value="Code"></DropDownListFieldSettings>
</SfDropDownList>
@code
{
[Parameter]
public List<TItemss> Country { get; set; }
private TVal _value { get; set; }
[Parameter]
public string ID { get; set; }
[Parameter]
public Expression<Func<TVal>> ValueExpression { get; set; }
[Parameter]
public TVal Value
{
get => _value;
set
{
if (!EqualityComparer<TVal>.Default.Equals(value, _value))
{
_value = value;
ValueChanged.InvokeAsync(value);
}
}
}
[Parameter]
public EventCallback<TVal> ValueChanged { get; set; }
[Parameter]
public EventCallback<ChangeEventArgs> InputHandler { get; set; }
}
[component.razor]
<DdlComp ID="Country" TVal="string" TItemss=" Countries" Country="@Countri">
</DdlComp>
@code {
public class Countries
{
public string Name { get; set; }
public string Code { get; set; }
}
List<Countries> Countri = new List<Countries>
{
new Countries() { Name = "Australia", Code = "AU" },
new Countries() { Name = "Bermuda", Code = "BM" },
new Countries() { Name = "Canada", Code = "CA" },
new Countries() { Name = "Cameroon", Code = "CM" },
new Countries() { Name = "Denmark", Code = "DK" },
new Countries() { Name = "France", Code = "FR" },
new Countries() { Name = "Finland", Code = "FI" }
};
} |
Hi Sureshkumar,
This is gold, i can combine it together with previous solution to get desired result.
Thank you!
Best regards,
Mairis