Thanks, it's crystal clear.While I was generalizing the code to match my full use case, I see that some information in OnCellEdit arguments are null :
- Cell
- Column
- ColumnObject
- Row
At least I miss one column object to define if column is "DisplayAsCheckBox".
|
public async Task OnCellEdit(CellEditArgs<OrdersDetails> args)
{
var ColIndex = await grid.GetColumnIndexByField(args.ColumnName);
var Column = await grid.GetColumns();
var ColumnObject = Column.ElementAt(Convert.ToInt32(ColIndex));
...
}
|
|
<SfGrid @ref="grid" TValue="CollectiviteTypeOperation" DataSource="@datasource" //changed DataSource property instead of dataSource
AllowSorting="true" AllowResizing=true EnableAltRow="true"
Width="100%" Height="100%">
. . .
<GridColumn Field="@nameof(CollectiviteTypeOperation.RevalorisationAutomatique)" Width="105px" DisplayAsCheckBox="true" TextAlign="@TextAlign.Center">
<EditTemplate>
<SfCheckBox ID="RevalorisationAutomatique" @bind-Checked=@CheckboxChecked /> //should be provide ID property as Field property. And binded separate bool variable
</EditTemplate>
</GridColumn>
<GridColumn Field="@nameof(CollectiviteTypeOperation.PresenceTypeRevalorisation)" Width="150px" DisplayAsCheckBox="true" TextAlign="@TextAlign.Center">
<EditTemplate>
<SfCheckBox ID="PresenceTypeRevalorisation" @bind-Checked=@CheckboxChecked2></SfCheckBox>//should be provide ID property as Field property. And binded separate bool variable
</EditTemplate>
</GridColumn>
. . .
</GridColumns>
</SfGrid>
@code {
. . .
public bool CheckboxChecked;
public bool CheckboxChecked2; //added bool property
public async void CellSelectHandler(CellSelectEventArgs<CollectiviteTypeOperation> args)
{
. . .
}
public async void OnCellEdit(CellEditArgs<CollectiviteTypeOperation> args)
{
var index = await grid.GetColumnIndexByField(args.ColumnName);
var columns = await grid.GetColumns();
if (columns[(int)index].DisplayAsCheckBox && columns[(int)index].Field == "RevalorisationAutomatique")
{
if (args.Value == "true")
{
CheckboxChecked = false; //Based on the value of the "Verified" column set the value for "bind-Checked" property
}
else
{
CheckboxChecked = true;
}
}
if (columns[(int)index].DisplayAsCheckBox && columns[(int)index].Field == "PresenceTypeRevalorisation")
{
if (args.Value == "true")
{
CheckboxChecked2 = false; //Based on the value of the "Verified" column set the value for "bind-Checked" property
}
else
{
CheckboxChecked2 = true;
}
}
}
public async void OnCellSave(CellSaveArgs<CollectiviteTypeOperation> args)
{
var index = await grid.GetColumnIndexByField(args.ColumnName);
var columns = await grid.GetColumns();
if (columns[(int)index].DisplayAsCheckBox && columns[(int)index].Field == "RevalorisationAutomatique")
{
if (CheckboxChecked)
{
args.Value = "true";
}
else
{
args.Value = "false";
}
}
if (columns[(int)index].DisplayAsCheckBox && columns[(int)index].Field == "PresenceTypeRevalorisation")
{
if (CheckboxChecked2)
{
args.Value = "true";
}
else
{
args.Value = "false";
}
}
}
}
|