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.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

how to add Combo box in the datagrid?

Thread ID:

Created:

Updated:

Platform:

Replies:

54493 Jan 10,2007 09:03 AM UTC Jan 16,2007 08:46 PM UTC Windows Forms 5
loading
Tags: GridControl
Administrator [Syncfusion]
Asked On January 10, 2007 09:03 AM UTC

hi,

i would like to know how to add a combo box in the datagrid(GridControl and GDBC)?

And when the value of the combobox is changed, it will caused the value of another column is changed also.

Thanks.

Administrator [Syncfusion]
Replied On January 10, 2007 09:17 AM UTC

Hi Lim,

Try this code.

StringCollection sc = new StringCollection();
sc.Add("One");
sc.Add("Two");
sc.Add("Three");
sc.Add("Four");
this.grid.Model.ColStyles[1].CellType = "ComboBox";
this.grid.Model.ColStyles[1].ChoiceList = sc;

Also refer our ComboBoxCells browser sample which illustrates how you can use ComboBoxes and Dropdown Multicolumn GridListControls in grid cells.
[install path]\Syncfusion\Essential Studio\4.4.0.49\windows\Grid.Windows\Samples\CellTypes\ComboboxCells\cs

Best Regards,
Haneef

Administrator [Syncfusion]
Replied On January 10, 2007 10:38 AM UTC

hi haneef,

What should i do in order to fulfill the requirement as below:

When the value of the combobox is changed, the value of the rest of the columns are changed based on the combobox's value.

Thanks!


Administrator [Syncfusion]
Replied On January 10, 2007 11:45 AM UTC

Hi Lim,

You can subscribe the CurrentCellChanged event of the grid and change the value of the rest of the column cells using the Renderer.ControlValue(it holds the newly changed value) through Model property of the grid. Here is a code snippet.

void gridDataBoundGrid1_CurrentCellChanged(object sender, EventArgs e)
{
GridDataBoundGrid grid = sender as GridDataBoundGrid;
GridCurrentCell cc = grid.CurrentCell;
GridComboBoxCellRenderer cr = cc.Renderer as GridComboBoxCellRenderer;
if (cr != null)
{
object EditState = cr.GetEditState();
for (int i = 1; i < grid.Model.RowCount; i++)
grid.Model[i, cc.ColIndex].CellValue = cr.ControlValue;
cr.SetEditState(EditState);
}
}

Best Regards,
Haneef

Administrator [Syncfusion]
Replied On January 16, 2007 07:42 AM UTC

hi haneef,

It's working!

Another problem is how to do validating on the user input in the combo box? If the input is not one of the item of the combo box's datasource, the user input is should be cancelled and rollback to the previous input..

Thanks

Administrator [Syncfusion]
Replied On January 16, 2007 08:46 PM UTC

Hi Lim,

Before changing the value of the rest of the column cells, you need to check the Renderer.ControlValue with the underlying datasource of the cell. Please refer the following code snippet.

void gridDataBoundGrid1_CurrentCellChanged(object sender, EventArgs e){
GridDataBoundGrid grid = sender as GridDataBoundGrid;
GridCurrentCell cc = grid.CurrentCell;
GridComboBoxCellRenderer cr = cc.Renderer as GridComboBoxCellRenderer;
if (cr != null){
object EditState = cr.GetEditState();
for (int i = 1; i < grid.Model.RowCount; i++)
{
if( CheckValue(cr.ControlValue,grid.Model[i, cc.ColIndex].ChoiceList))
grid.Model[i, cc.ColIndex].CellValue = cr.ControlValue;
}
cr.SetEditState(EditState);
}
}

private bool CheckValue(object svalue,StringCollection sc){
return svalue != null && sc != null && sc.Contains(svalue.ToString());
}

Best Regards,
Haneef

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;