Fill row of DataGrid base on selected combobox item

Hi,
I need fill SfDataGrid's row based on data in combobox in first cell of current row. Code bellow works, but only for one value "Description". 

<syncfusion:SfDataGrid x:Name="DataGrid"
                           
                             ItemsSource="{Binding Delivery}"
                             AllowGrouping="True"
                             AllowDeleting="True"
                             SelectionUnit="Row"
                             EditTrigger="OnTap"
                             AutoGenerateColumns="False"
                             ShowGroupDropArea="True"
                             AllowFiltering="True"
                             AllowEditing="True"
                             NewItemPlaceholderPosition="AtBeginning"
                             ShowRowHeader="True"
                             AddNewRowPosition="Top">
 
            <syncfusion:SfDataGrid.Columns>
 
                <syncfusion:GridComboBoxColumn 
                                             HeaderText="Material"
                                             SelectedValuePath="Description"
                                             DisplayMemberPath="Name"
                                             ItemsSource="{Binding MaterialParts}"
                                             MappingName="Description">
                </syncfusion:GridComboBoxColumn>
 
                <syncfusion:GridTextColumn HeaderText="Description"
                                         IsReadOnly="True"
                                         MappingName="Description" />
 
                <syncfusion:GridTextColumn HeaderText="Quantity"
                                         MappingName="Qty" />
 
 
            </syncfusion:SfDataGrid.Columns>
 
        </syncfusion:SfDataGrid>

5 Replies

SS Susmitha Sundar Syncfusion Team March 10, 2020 12:21 PM UTC

Hi Ondrej, 
 
Thank you for using Syncfusion controls. 
 
By default, when you change the GridComboBoxColumn, it does not change the other columns values based on ComboBox selected value. You need to manually change the column value. You can achieve this by using SfDataGrid.CurrentCellDropDownSelectionChanged event. Please refer to the below code, 

private void SfDataGrid_CurrentCellDropDownSelectionChanged(object sender, CurrentCellDropDownSelectionChangedEventArgs e) 
{ 
    var recordindex = this.sfDataGrid.ResolveToRecordIndex(e.RowColumnIndex.RowIndex); 
    var record = this.sfDataGrid.View.Records[recordindex]; 
    var data = record.Data as Materials; 
    data.Description = (e.SelectedItem as Materials).Description; 
    data.Quantity = (e.SelectedItem as Materials).Quantity; 
} 
 
 
<syncfusion:SfDataGrid x:Name="sfDataGrid"  
                       AllowEditing="True" 
                       ItemsSource="{Binding Path=MaterialCollection}" 
                       CurrentCellDropDownSelectionChanged="SfDataGrid_CurrentCellDropDownSelectionChanged" 
                       AutoGenerateColumns="False"> 
    <syncfusion:SfDataGrid.Columns> 
        <syncfusion:GridComboBoxColumn MappingName="Name" ItemsSource="{Binding MaterialCollection}" SelectedValuePath="Name" DisplayMemberPath="Name" /> 
        <syncfusion:GridTextColumn MappingName="Description"/>                
        <syncfusion:GridTextColumn MappingName="Quantity" /> 
    </syncfusion:SfDataGrid.Columns> 
</syncfusion:SfDataGrid> 

 

If we misunderstood anything, please modify the above sample based on your requirement and get back to us with following details,  
 
1.       Syncfusion version 
2.       Issue reproducing video (if possible) 
 
Regards, 
Susmitha S 



ON Ondrej March 10, 2020 10:25 PM UTC

Thank you for your quick response. Your example works fine, but only if ObservableCollection contains data. Not working with option "AddNewRowPosition = Top". Any solution?

Thanks a lot

Best regards
Ondrej 


SS Susmitha Sundar Syncfusion Team March 11, 2020 11:23 AM UTC

Hi Ondrej, 
 
Thank you for your update. 
 
You can achieve this by create the instance for new row in SfDataGrid.AddNewRowInitiating event and set the value in SfDataGrid_CurrentCellDropDownSelectionChanged. Please refer to the below code, 
 
private void SfDataGrid_CurrentCellDropDownSelectionChanged(object sender, CurrentCellDropDownSelectionChangedEventArgs e) 
{ 
    if (this.sfDataGrid.IsAddNewIndex(e.RowColumnIndex.RowIndex)) 
    { 
        data.Description = (e.SelectedItem as Materials).Description; 
        data.Quantity = (e.SelectedItem as Materials).Quantity; 
    } 
    else 
    { 
        var recordindex = this.sfDataGrid.ResolveToRecordIndex(e.RowColumnIndex.RowIndex); 
        var record = this.sfDataGrid.View.Records[recordindex]; 
        var rowdata = record.Data as Materials; 
        rowdata.Description = (e.SelectedItem as Materials).Description; 
        rowdata.Quantity = (e.SelectedItem as Materials).Quantity; 
    } 
} 
 
 
 
private void SfDataGrid_AddNewRowInitiating(object sender, AddNewRowInitiatingEventArgs e) 
{ 
    data = e.NewObject as Materials; 
} 
 
 
Please check the sample and let us know if you need further assistance on this. 
 
Regards, 
Susmitha S 



ON Ondrej March 21, 2020 11:55 AM UTC

It's working now. Thanks for help


SS Susmitha Sundar Syncfusion Team March 23, 2020 05:48 AM UTC

Hi Ondrej, 
 
Thanks for the update. 
 
We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you. 
 
Regards, 
Susmitha S 


Loader.
Up arrow icon