Hi,
I have a problem with SfSwitchControl in SFDatagrid
So I have a data like this.
what I want to achive is that 2 switchcontrol above is enable/alwoedit when the OK and NG column is 0
but if one of those 2 column (OK or NG) is 1 then the 2 switchcontrol is disable/disallow to edit.
I have no proble if the data is not much, but the prblem is when i have so many data so i have to scroll down to see other data.
For example if i scroll down the data, i have data that OK & NG column both are 0. So the switch should be enable. But now, the NG switch is disable, and the OK switch enable
here's my xaml.cs class
public partial class InputHasilRepairStep2 : ContentPage
{
InputHasilRepairStep2ViewModel viewModel;
// Field to notify that the SfDataGrid has been loaded in view
public static bool isGridLoaded = false;
public InputHasilRepairStep2()
{
InitializeComponent();
viewModel = BindingContext as InputHasilRepairStep2ViewModel;
viewModel.InitPage = true;
hasilRepariGrid.GridLoaded += DataGrid_GridLoaded;
hasilRepariGrid.CellRenderers.Remove("Switch");
hasilRepariGrid.CellRenderers.Add("Switch", new HasilSwitch());
}
// Field to notify that the SfDataGrid has been loaded in view
private void DataGrid_GridLoaded(object sender, GridLoadedEventArgs e)
{
isGridLoaded = true;
}
}
and here is HasilSwitch class
public class HasilSwitch : GridCellSwitchRenderer
{
protected override SfSwitchControl OnCreateDisplayUIView()
{
var switchControl = new SfSwitchControl();
switchControl.PropertyChanged += SwitchControl_PropertyChanged;
switchControl.IsEnabled = true;
return switchControl;
}
public override void OnInitializeDisplayView(DataColumnBase dataColumn, SfSwitchControl view)
{
base.OnInitializeDisplayView(dataColumn, view);
string sysDNo = (dataColumn.RowData as T_REPAIR_DET).SYS_D_NO.ToString();
view.Toggled += View_Toggled;
view.PropertyChanged += SwitchControl_PropertyChanged;
if (view is SfSwitchControl)
{
(view as SfSwitchControl).IsEnabled = true;
}
}
private void sfFocused(object sender, FocusEventArgs e)
{
}
private void View_Toggled(object sender, Xamarin.Forms.ToggledEventArgs e)
{
// Checked condition to skip execution of codes before SfDataGrid is loaded
if (InputHasilRepairStep2.isGridLoaded)
{
var data = ((sender as SfSwitchControl).Parent as GridCell).DataColumn.RowData as T_REPAIR_DET;
string sysdNo = (((sender as SfSwitchControl).Parent as GridCell).DataColumn.RowData as T_REPAIR_DET).SYS_D_NO.ToString();
if (((sender as SfSwitchControl).Parent as GridCell).DataColumn.GridColumn.MappingName == "NG_F_B" ||
((sender as SfSwitchControl).Parent as GridCell).DataColumn.GridColumn.MappingName == "OK_F_B")
{
if (((((sender as SfSwitchControl).Parent as GridCell).DataColumn.RowData as T_REPAIR_DET).OK_F_B_ORI == true &&
(((sender as SfSwitchControl).Parent as GridCell).DataColumn.RowData as T_REPAIR_DET).NG_F_B_ORI == false) ||
((((sender as SfSwitchControl).Parent as GridCell).DataColumn.RowData as T_REPAIR_DET).OK_F_B_ORI == false &&
(((sender as SfSwitchControl).Parent as GridCell).DataColumn.RowData as T_REPAIR_DET).NG_F_B_ORI == true))
{
(sender as SfSwitchControl).IsEnabled = false;
}
else
{
(sender as SfSwitchControl).IsEnabled = true;
}
}
}
}
private void SwitchControl_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Parent")
{
string sysdNo = (((sender as SfSwitchControl).Parent as GridCell).DataColumn.RowData as T_REPAIR_DET).SYS_D_NO.ToString();
if (((sender as SfSwitchControl).Parent as GridCell).DataColumn.GridColumn.MappingName == "NG_F_B" ||
((sender as SfSwitchControl).Parent as GridCell).DataColumn.GridColumn.MappingName == "OK_F_B")
{
if (((((sender as SfSwitchControl).Parent as GridCell).DataColumn.RowData as T_REPAIR_DET).OK_F_B_ORI == true &&
(((sender as SfSwitchControl).Parent as GridCell).DataColumn.RowData as T_REPAIR_DET).NG_F_B_ORI == false) ||
((((sender as SfSwitchControl).Parent as GridCell).DataColumn.RowData as T_REPAIR_DET).OK_F_B_ORI == false &&
(((sender as SfSwitchControl).Parent as GridCell).DataColumn.RowData as T_REPAIR_DET).NG_F_B_ORI == true))
{
(sender as SfSwitchControl).IsEnabled = false;
}
else
{
(sender as SfSwitchControl).IsEnabled = true;
}
}
}
}
}
Hi Galih,
You can achieve your requirement ”How to enable or disable SfSwitchControl based on the data ” by updating the SfSwitch control IsEnabled property in ScrollStateChanged event while scrolling based on required cell value of row instead of SfSwitch control property changed as like below code snippet.
Code Snippet:
|
public DataGridPage() { InitializeComponent(); sfGrid.ScrollStateChanged += SfGrid_ScrollStateChanged; sfGrid.GridLoaded += SfGrid_GridLoaded;
}
private void SfGrid_ScrollStateChanged(object sender, ScrollStateChangedEventArgs e) { UpdateSwitchControl(); }
private void SfGrid_GridLoaded(object sender, GridLoadedEventArgs e) { isGridLoaded = true; UpdateSwitchControl(); }
private void UpdateSwitchControl() { var val = sfGrid.GetRowGenerator().Items; foreach (var datarow in sfGrid.GetRowGenerator().Items) { var wholeRowElement = (VirtualizingCellsControl)datarow.GetType().GetRuntimeFields().FirstOrDefault(x => (x.Name.Equals("WholeRowElement")))?.GetValue(datarow); if (datarow.RowType == RowType.DefaultRow) { if (((datarow.RowData as OrderInfo).IsChecked == true && (datarow.RowData as OrderInfo).IsClosed == false) || (datarow.RowData as OrderInfo).IsChecked == false && (datarow.RowData as OrderInfo).IsClosed == true) { if (wholeRowElement != null) { foreach (var col in wholeRowElement.Children) { if (col is GridCell gridCell) { if (gridCell.Content is SfSwitchControl switchControl) { switchControl.IsEnabled = false; } } } } }
else { if (wholeRowElement != null) { foreach (var col in wholeRowElement.Children) { if (col is GridCell gridCell) { if (gridCell.Content is SfSwitchControl switchControl) { switchControl.IsEnabled = true; } } } } } } } } } |
We have attached a modified sample for your reference. Please have a look at this sample and revert to us more details about your requirement with illustration video, if we misunderstood your requirements.
Regards,
Suja
Got It.. Its work.. Cool!
Thank you Suja.. Have a nice day..
Hi Galih,
Thank you for the update.
We are glad that our solution meets your requirement. Please let us know if you need further assistance. As always, we are happy to help you out.
Regards,
Suja