We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Update columns after selecting GridComboBoxColumn

Hi,


I have an SfDataGrid with four columns: Product, Unit, Price, Quantity


The product column is a GridComboBoxColumn, what I intend to do when selecting a product fill in the unit and price columns that are in the GridComboBoxColumn


1 Reply

SJ Sathiyathanam Jeyakumar Syncfusion Team November 16, 2022 10:48 AM

We suspect your requirement is to update the relevant UnitPrice and Qunatity for a selected product. You can achieve your requirement by add the UnitPrice and Quantity in a Dictionary with the key as Product name . Then you can listen the selection changed in a GridComboboxColumn by using CurrentCellDropDownSelectionChanged event and here you can update the UnitPrice and Quantity based on the selected product.


ViewModel.cs

public class ViewModel

{

    public ViewModel()

    {

        GetListOrdersDetails();

    }

 

    public Dictionary<string, double> UnitPriceDict { get; set; }

    public Dictionary<string, int> QuantityDict { get; set; }

    public List<string> ComboItems { get; set; }

 

    private ObservableCollection<OrderInfo> _ordersListDetails;

 

    /// <summary>

    /// Gets or sets the orders details.

    /// </summary>

    /// <value>The orders details.</value>

    public ObservableCollection<OrderInfo> OrdersListDetails

    {

        get { return _ordersListDetails; }

        set { _ordersListDetails = value; }

    }

    public void GetListOrdersDetails()

    {

        OrdersListDetails = new ObservableCollection<OrderInfo>();

        ComboItems = productName.ToList();

        UpdateUnitPriceDictionary();

        UpdateQuantityDictionary();

        OrdersListDetails.Add(new OrderInfo() { ProductName = "Alice Mutton",UnitPrice = UnitPriceDict["Alice Mutton"] ,Quantity = QuantityDict["Alice Mutton"] });

        OrdersListDetails.Add(new OrderInfo() { ProductName = "NuNuCa Nuß-Nougat-Creme", UnitPrice = UnitPriceDict["NuNuCa Nuß-Nougat-Creme"], Quantity = QuantityDict["NuNuCa Nuß-Nougat-Creme"] });

        OrdersListDetails.Add(new OrderInfo() { ProductName = "Boston Crab Meat", UnitPrice = UnitPriceDict["Boston Crab Meat"], Quantity = QuantityDict["Boston Crab Meat"] });

        OrdersListDetails.Add(new OrderInfo() { ProductName = "Raclette Courdavault", UnitPrice = UnitPriceDict["Raclette Courdavault"], Quantity = QuantityDict["Raclette Courdavault"] });

        OrdersListDetails.Add(new OrderInfo() { ProductName = "Wimmers gute", UnitPrice = UnitPriceDict["Wimmers gute"], Quantity = QuantityDict["Wimmers gute"] });

 

    }

    private void UpdateUnitPriceDictionary()

    {

        UnitPriceDict = new Dictionary<string, double>();

        UnitPriceDict.Add("Alice Mutton", 28.5);

        UnitPriceDict.Add("NuNuCa Nuß-Nougat-Creme", 336.2);

        UnitPriceDict.Add("Boston Crab Meat", 88.3);

        UnitPriceDict.Add("Raclette Courdavault", 86);

        UnitPriceDict.Add("Wimmers gute", 512);

    }

    private void UpdateQuantityDictionary()

    {

        QuantityDict = new Dictionary<string, int>();

        QuantityDict.Add("Alice Mutton", 10);

        QuantityDict.Add("NuNuCa Nuß-Nougat-Creme", 20);

        QuantityDict.Add("Boston Crab Meat", 30);

        QuantityDict.Add("Raclette Courdavault", 40);

        QuantityDict.Add("Wimmers gute", 50);

    }

      

 

    /// <summary>

    /// Collection of ProductNames

    /// </summary>

    string[] productName = new string[]

    {

        "Alice Mutton",

        "NuNuCa Nuß-Nougat-Creme",

        "Boston Crab Meat",

        "Raclette Courdavault",

        "Wimmers gute",

    };

 

}

 


XAML && C#

<syncfusion:SfDataGrid Name="dataGrid"

                    AllowEditing="True"

                    AutoGenerateColumns="False"

                   CurrentCellDropDownSelectionChanged="dataGrid_CurrentCellDropDownSelectionChanged"

                    ItemsSource="{Binding OrdersListDetails}" >

 

private void dataGrid_CurrentCellDropDownSelectionChanged(object sender, CurrentCellDropDownSelectionChangedEventArgs e)

{

    var sfdatagrid = sender as SfDataGrid;

    var viewModel = sfdatagrid.DataContext as ViewModel;

    int rowIndex = sfdatagrid.ResolveToRecordIndex(e.RowColumnIndex.RowIndex);

    var record = (sfdatagrid.View.Records[rowIndex] as RecordEntry).Data as OrderInfo;

    record.UnitPrice = viewModel.UnitPriceDict[e.SelectedItem.ToString()];

    record.Quantity = viewModel.QuantityDict[e.SelectedItem.ToString()];

}


You can download the sample from the attachments.


Attachment: SfDataGrid_Net_47174d5.zip

Loader.
Live Chat Icon For mobile
Up arrow icon