Selecting an item from GridComboBoxColumn programatically

Hello,

I have an SfDataGrid that I use for creating purchase orders. As such, usually the user adds new rows as required and selects a product using a ComboBox. Image below (sorry text is in Spanish).


However, I now need to fill this table automatically without any user input. I have gone so far as to making the rows show up correctly... except for the ComboBox column. It shows up blank (and the CellComboBoxSelectionChanged event is not fired as a consequence, making other columns like price not update correctly).


I cannot seem to find a way to change the selected item or index of the GridComboBoxColumn. How may I achieve this?

If it is worth mentioning, the GridComboBoxColumn.DataSource is an object.


Any help is appreciated, thanks.


3 Replies

MA Mohanram Anbukkarasu Syncfusion Team August 6, 2021 12:50 PM UTC

Hi Gerardo, 

Thanks for contacting Syncfusion support.  

We regret to let you know that we are little unclear about your requirement. We suspect that your requirement is to fill a default value for the GridComboBoxColumn whilc adding a new row (Using AddNewRow), without opening the dropdown and selection a value from the ComboBox. Kindly revert to us with more details if we have misunderstood your requirement. We will update with further details based on your confirmation.  

Regards, 
Mohanram A. 



GE Gerardo replied to Mohanram Anbukkarasu August 6, 2021 04:43 PM UTC

Hi, Mohanram 


I will try my best to explain myself.

I have an SfDataGrid that has a GridComboBoxColumn. I want to change the selected item of the ComboBox programmatically so that the CellComboBoxSelectionChanged event fires. I can set the text in the column but this does not fire said event.


Cheers

Gerardo



MA Mohanram Anbukkarasu Syncfusion Team August 9, 2021 02:18 PM UTC

Hi Gerardo, 

Thanks for the update.  

CellComboBoxSelectionChanged will be fired only when the SelectedItem of the ComboxBox in the cell is changed. You can programmatically trigger this event by programmatically edit the required cell in the GridComboBoxColumn using the cell index and change the SelectedIndex/ SelectedItem of the cell’s ComboBox programmatically as shown in the following code example.  

Code example :  

private void button1_Click(object sender, System.EventArgs e) 
{ 
    this.sfDataGrid1.MoveToCurrentCell(new RowColumnIndex(1, 6)); 
    this.sfDataGrid1.CurrentCell.BeginEdit(); 
    this.sfDataGrid1.BeginInvoke(new Action(() => 
    { 
        (this.sfDataGrid1.CurrentCell.CellRenderer.CurrentCellRendererElement as SfComboBox).SelectedIndex = 3; 
    })); 
} 


Please let us know if you require any other assistance from us.  

Regards, 
Mohanram A. 


Loader.
Up arrow icon