By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,658 Members | 1,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,658 IT Pros & Developers. It's quick & easy.

DataGrid: Custom ComboBox column problem

P: n/a
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.
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

I resolved this by comparing the iCurrentRow member and rowNum parameter, if
they're equal I do not do anything inside the Edit override.

TT.

"TT (Tom Tempelaere)" wrote:
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.

Nov 17 '05 #2

P: n/a
Oops,

This gives other problems: When I do not change the current row in the grid
then I can't edit the value anymore ...

TT

"TT (Tom Tempelaere)" wrote:
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.

I resolved this by comparing the iCurrentRow member and rowNum parameter, if
they're equal I do not do anything inside the Edit override.

TT.

"TT (Tom Tempelaere)" wrote:
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.

Nov 17 '05 #3

P: n/a
Hi,

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 );
}

TT
"TT (Tom Tempelaere)" wrote:
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.

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.