Hay there,
I'm writing my own DataGridComboBoxColumn because .NET 1.1 does not have one
(I hope .NET 2.0 supplies one). I based it on this article:
http://msdn.microsoft.com/msdnmag/is...s/default.aspx
I have a problem when there are two DataGrid's on one form, and when I
switch focus from one grid to the other. To be more precise, when I'm editing
a combo box column in one grid, and then click in the combo column of another
grid an exception is thrown stating "The ListManager's position must be equal
to rowNum.
Parameter name: rowNum".
I will post the code if anyone feels like digging into it. Anyway, perhaps
someone has a better link with a how-to?
Notes about the code:
* The only thing that the PropertyEditorTable is used for in this context is
for setting the DataSource of the ComboBox that edits the value.
* The exception occurs when executing SetColumnValueAtRow in the
comboBox_Leave event handler
internal class PropertyComboBoxColumn : DataGridTextBoxColumn
{
protected PropertyEditorTable pe;
private ComboBox comboBox;
private CurrencyManager cm;
private int iCurrentRow;
public ComboBox ComboBox {
get { return comboBox; }
}
public PropertyComboBoxColumn( PropertyEditorTable pe )
{
this.pe = pe;
this.cm = null;
this.comboBox = new ComboBox();
this.comboBox.DropDownStyle = ComboBoxStyle.DropDown;
this.comboBox.Leave += new EventHandler( comboBox_Leave );
}
protected override void Edit (
CurrencyManager source,
int rowNum,
Rectangle bounds,
bool readOnly,
string instantText,
bool cellIsVisible )
{
base.Edit( source, rowNum, bounds, readOnly, instantText, cellIsVisible );
if( !readOnly && cellIsVisible )
{
iCurrentRow = rowNum;
cm = source;
DataGridTableStyle.DataGrid.Scroll += new EventHandler( DataGrid_Scroll );
comboBox.Parent = TextBox.Parent;
comboBox.Location =
DataGridTableStyle.DataGrid.GetCurrentCellBounds() .Location;
comboBox.Size = new Size( TextBox.Size.Width, comboBox.Size.Height );
pe.SetDataSourceForPropertyCombo( rowNum, source, comboBox );
int foundIndex = comboBox.FindStringExact( TextBox.Text );
if( foundIndex == -1 )
comboBox.Text = TextBox.Text;
else
comboBox.SelectedIndex = foundIndex;
comboBox.Show();
comboBox.BringToFront();
comboBox.Focus();
}
}
private void DataGrid_Scroll(object sender, EventArgs e)
{
comboBox.Hide();
}
private void comboBox_Leave( object sender, EventArgs e )
{
SetColumnValueAtRow( cm, iCurrentRow, comboBox.Text );
Invalidate();
comboBox.Hide();
DataGridTableStyle.DataGrid.Scroll -= new EventHandler( DataGrid_Scroll );
}
}
Thanks,
--
Tom Tempelaere.