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

Clearing ComboBox on a grid

I am having problems with a combobox that I have for a grid. When I clear the text for that cell which has the combobox, the value is treated as 'illegal' since an empty string is not one of the items on the list. I want to keep AutoComplete and make the choices exclusive to the list but I also need to clear the selected item. When the selected text is cleared it has the effect of making the entry invalid, thus disabling entry in the grid until the invalid string is resolved. Is there a easy way to clear this value like in a combox box you would set the selectedindex to -1. thanks! tim

10 Replies

AD Administrator Syncfusion Team July 3, 2003 02:24 PM UTC

> I am having problems with a combobox that I have for a grid. When I clear the text for that cell which has the combobox, the value is treated as 'illegal' since an empty string is not one of the items on the list. I want to keep AutoComplete and make the choices exclusive to the list but I also need to clear the selected item. When the selected text is cleared it has the effect of making the entry invalid, thus disabling entry in the grid until the invalid string is resolved. Is there a easy way to clear this value like in a combox box you would set the selectedindex to -1. > > thanks! > > tim > With 1.6.1, if the cell does not have focus, you can just use an indexer to set it blank and this will blank out the cell. this.gridControl1[5,3].Text = ""; If you want to blank it out typeing, here is a try at it handling the CurrentCellValidating event.
private void gridControl1_CurrentCellValidating(object sender, System.ComponentModel.CancelEventArgs e)
{
	GridCurrentCell cc = this.gridControl1.CurrentCell;
	if(cc.Renderer is GridComboBoxCellRenderer && cc.Renderer.ControlText == "")
	{
		cc.CancelEdit();
	 	this.gridControl1[cc.RowIndex, cc.ColIndex].Text = "";
	}
}


TJ Tim Jackson July 7, 2003 07:49 AM UTC

The problem with what you suggest (which is what I am already doing) is that the blank text is treated as in illegal value since "" is not one of the selections in the the drop down combo list. The result is the grid is treating that entry as invalid and because of that, you cannot make a change to any other cell in the grid till it is resolved i.e. a valid non blank selection is made on that combo box. HEEEELPP!!! :) Tim > > With 1.6.1, if the cell does not have focus, you can just use an indexer to set it blank and this will blank out the cell. > > this.gridControl1[5,3].Text = ""; > > If you want to blank it out typeing, here is a try at it handling the CurrentCellValidating event. >
> private void gridControl1_CurrentCellValidating(object sender, System.ComponentModel.CancelEventArgs e)
> {
> 	GridCurrentCell cc = this.gridControl1.CurrentCell;
> 	if(cc.Renderer is GridComboBoxCellRenderer && cc.Renderer.ControlText == "")
> 	{
> 		cc.CancelEdit();
> 	 	this.gridControl1[cc.RowIndex, cc.ColIndex].Text = "";
> 	}
> }
> 
>


AD Administrator Syncfusion Team July 7, 2003 10:38 AM UTC

Attached is a sample.


JM Jose M. Gonzalez July 7, 2003 05:35 PM UTC

I have a similar situation but the ComboBox is containing a collection of objects, if the user deletes the cell value I want to set the cell value to NULL. The following code is giving me an error (Object type cannot be converted to target type) when I try to delete the cell value. private void gridDataBound_CurrentCellDeleting(object sender, System.ComponentModel.CancelEventArgs e) { GridCurrentCell cc = this.gridDataBound.CurrentCell; if(cc.Renderer is GridComboBoxCellRenderer && cc.Renderer.ControlValue != null) { cc.Renderer.Control.Text = String.Empty; gridDataBound[cc.RowIndex, cc.ColIndex].CellValue = null; } }


AD Administrator Syncfusion Team July 7, 2003 06:18 PM UTC

You might try using DBNull.Value insteadof null to see if that will work for you.


JM Jose M. Gonzalez July 7, 2003 07:19 PM UTC

> You might try using DBNull.Value insteadof null to see if that will work for you. > It didn't work. System.ArgumentException: Object type cannot be converted to target type. at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value) at Syncfusion.Windows.Forms.Grid.GridModelDataBinder.SaveCellInfo(GridSaveCellInfoEventArgs e) catched at Syncfusion.Windows.Forms.Grid.GridModelDataBinder.SaveCellInfo(GridSaveCellInfoEventArgs e) in :line 0 An unhandled exception of type 'System.ArgumentException' occurred in syncfusion.grid.dll Additional information: Object type cannot be converted to target type.


AD Administrator Syncfusion Team July 8, 2003 09:15 AM UTC

This exception comes from SaveCellInfo method in GridModelDataBinder because you are trying to assing System.DBNull to a class property which is an object. Most of my data sources are BindingList not DataTables, how can I set "null" value instead of "System.DBNull"? THanks


AD Administrator Syncfusion Team July 8, 2003 03:57 PM UTC

I haven't really tried this but I think you could try handling the grid.Model.DataProviderSaveCellInfo event and swap out DBNull as follows: if (e.Style.CellValue is DBNull) e.Style.CellValue = null; Do not change e.Handled so that GridModelDataBinder.SaveCellInfo will still be called afterwards. Stefan


AD Administrator Syncfusion Team July 8, 2003 10:27 PM UTC

NOT IT WORKS!!!! Thanks......


AD Administrator Syncfusion Team July 8, 2003 10:27 PM UTC

NOW IT WORKS!!!! Thanks++++

Loader.
Live Chat Icon For mobile
Up arrow icon