DataGrid: Custom ComboBox column problem

G

Guest

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/issues/03/08/DataGrids/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,
 
G

Guest

Hi,

It appears that the Edit method is called twice when a user clicks on a
DataGrid: one time for the current cell, and then immediately for the newly
clicked cell. This results in comboBox_leave being called twice, and that
gives the exception when executing SetColumnValueAtRow.

This solution seems to do better. Change the comboBox_leave handler to:

private void comboBox_Leave( object sender, EventArgs e )
{
if( cm.Position == iCurrentRow )
SetColumnValueAtRow( cm, iCurrentRow, comboBox.Text );
Invalidate();
comboBox.Hide();
DataGridTableStyle.DataGrid.Scroll -= new EventHandler( DataGrid_Scroll );
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top