I am currently using Syncfusion Sf Datagrid. I want to display the Datagrids based on count. For example I have button A and button B, if user click on A, it will display 1 datagrid. If user click on B, it will display 2 datagrid. FYI, the datagrid will display on the same page.
What my code does now is displaying all datagrid. I would like to display the datagrid only based on condition (in this case which is A or B). I am not sure how to achieve this because the datagrid is in xaml, how do I make sure the correct count of datagrid I want to appear? Let's say if I want to have more category that has different items, how do I display the correct number of datagrid according to the items inside a category?
MainPage.xaml
<ContentPage.Content>
<StackLayout>
<Button Text="A"/>
<Button Text="B"/>
<sfgrid:SfDataGrid ItemsSource="{Binding fruitA}"/>
<sfgrid:SfDataGrid ItemsSource="{Binding vegeB1}"/>
<sfgrid:SfDataGrid ItemsSource="{Binding vegeB2}"/>
</StackLayout>
</ContentPage.Content>
MainPage.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
this.BindingContext = new ViewModel();
}
}
ViewModel
public class ViewModel
{
public ObservableCollection<Test> fruitA { get; set; }
public ObservableCollection<Test> vegeB1 { get; set; }
public ObservableCollection<Test> vegeB2 { get; set; }
public ViewModel()
{
fruitA = new ObservableCollection<ChildPart>();
fruitA.Add(new Test() { Title = "Orange", Value = "1.20" });
fruitA.Add(new Test() { Title = "Banana", Value = "1.40" });
fruitA.Add(new Test() { Title = "Apple", Value = "1.30" });
vegeB1 = new ObservableCollection<ChildPart>();
vegeB1.Add(new Test() { Title = "Spinach", Value = "1.20" });
vegeB1.Add(new Test() { Title = "Cabbage", Value = "1.40" });
vegeB2 = new ObservableCollection<ChildPart>();
vegeB2.Add(new Test() { Title = "Lettuce", Value = "1.20" });
vegeB2.Add(new Test() { Title = "Broccoli", Value = "1.40" });
vegeB2.Add(new Test() { Title = "Celery", Value = "1.30" });
}
}
Test
public class Test
{
public string Title { get; set; }
public string Value { get; set; }
}
IsVisible
property of the Datagrid? You can use this to bind a property from the ViewModel and this will change with the button Click Command. – pinedax