GridAwareTextBoxes and exclusive combo boxes

Hi, I''m getting an exception when the user tries typing in an invalid value in the GridAwareTextBox for an exclusive combo box (details below). What is the best way of handling this problem? Thanks, Sue An unhandled exception of type ''System.ArgumentException'' occurred in system.windows.forms.dll Additional information: ''-1'' is not a valid value for ''length''. The exception is occuring in the OnTextChanged event in GridAwareTextBox where it is trying to set the text box selection when the validation fails. The problem is fairly easily reproducable: Simply add the following code into the FormulaGrid example Load event, and then try to type an invalid value in the grid aware text box for the cell: int col1 = 2; int row1 = 14; this.gridControl1[row1,col1].Format = string.Empty; this.gridControl1[row1,col1].CellType = "ComboBox"; System.Collections.Specialized.StringCollection list = new System.Collections.Specialized.StringCollection(); list.AddRange(Enum.GetNames(typeof(System.DayOfWeek))); this.gridControl1[row1,col1].ChoiceList = list; this.gridControl1[row1,col1].ShowButtons = Syncfusion.Windows.Forms.Grid.GridShowButtons.ShowCurrentCell; this.gridControl1[row1,col1].DropDownStyle = GridDropDownStyle.Exclusive; this.gridControl1[row1,col1].CellValue = System.DayOfWeek.Wednesday;

1 Reply

AD Administrator Syncfusion Team October 17, 2005 08:07 AM UTC

One way you can handle this is to override the OnTextChanged method in a derived GridAwareTextBox and do work there to make sure only list items appear for this type of cell. Here is any initial try at this. Depending upon the exact behavior you want, you might have to add/remove code to handle other such behavior.
public class MyGridAwareTextBox : GridAwareTextBox
{
	protected override void OnTextChanged(EventArgs e)
	{
		GridCurrentCell cc = this.grid.CurrentCell;
		if(cc != null)
		{
			GridStyleInfo style = this.grid.Model[cc.RowIndex, cc.ColIndex];
			if(style.CellType == "ComboBox" && style.DropDownStyle == GridDropDownStyle.Exclusive)
			{
				GridComboBoxCellRenderer cr = cc.Renderer as GridComboBoxCellRenderer;
				int found = cr.ListBoxPart.FindStringExact(this.Text);
				if(found == -1)
				{
					found = cr.ListBoxPart.FindString(this.Text);
					if(found == -1)
					{
						this.Text = cc.Renderer.ControlText;
						return;
					}
					else
					{
						int pos = this.SelectionStart;
						this.Text = cr.ListBoxPart.Items[found].ToString();
						this.SelectionStart = pos;
						this.SelectionLength = this.Text.Length - pos;
					}
				}
			}
		}
		base.OnTextChanged (e);
	}

	GridControlBase grid;
	// WireGrid modified to get a reference to the grid.
	public new void WireGrid(GridControlBase grid)
	{
		if (grid != null)
		{
			this.grid = grid;
			base.WireGrid(grid);
		}
	}
}

Loader.
Up arrow icon