473,395 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

DataGrid: Custom ComboBox column problem

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
3 6795
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bill C. | last post by:
Hi, I'm trying to implement a ComboBox drop-down column for a DataGrid. When a cell is selected in the ComboBox column I overlay a ComboBox over the cell and call: this.comboBox.Show();...
2
by: john sutor | last post by:
Does anyone know how to create a combobox in a standard datagrid? I can create check boxes , but not the combobox
0
by: RFickling | last post by:
Hi, I have a DataGrid with custom ComboBox and DataTimePicker controls in column styles. When I add a new row, typing values into all columns, then close the form, I get a...
0
by: metamedia | last post by:
How do I get a datagrid to register a data change from a custom control cell (combobox) I've got a Windows Application with a Windows Form Datagrid that has a custom combobox column. When the user...
3
by: PeterZ | last post by:
G'day, After doing much searching and pinching bits of ideas from here there and everywhere I came up with a fairly 'clean' solution of including a comboBox into a dataGrid column. You can...
3
by: Doug | last post by:
Hi I have the following code (not mine) that populates a datagrid with some file names. But I want to replace the datagrid with a combo box. private void OnCurrentDataCellChanged(object sender,...
6
by: Ron L | last post by:
I have a dataset whose source is a SQL 2k stored procedure that I am trying to display in a datagrid. This datasource has 4 columns that I am interested in here, a text column and 3 value columns...
6
by: Doug Bell | last post by:
Hi I have a datagrid with a combo box, I need to populate the combo with data dependant on the record value. eg for record 1, field Warehouse = 2R so combo would allow selection of locations...
0
by: simchajoy2000 | last post by:
Hi, I am trying to design a custom datagrid column that will allow me to put a combobox of colors in it. Well actually I can get that far but I can't figure out a way to save the color in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.