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

Databound combobox, new and selected value confusion.

P: n/a
I have a combobox that is very much like the one found in the RSS
project here:
http://msdn.microsoft.com/vstudio/ex...harp/learning/

My projectNameComboBox basically is filled with a list of values from a
table, and as the user selects values, a datagrid displays related
records from another table because it is bound via FK relationship.

My table:
/****** Object: Table [dbo].[ProjectNames] Script Date: 06/19/2006
19:46:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ProjectNames](
[ProjectName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL,
CONSTRAINT [PK_ProjectNames] PRIMARY KEY CLUSTERED
(
[ProjectName] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
I have the combobox bound to projectNamesBindSource, and projectNames
is the single column of names of projects. Display Member and Value
Member are both bound to ProjectName, which is the column in the
ProjectNames table. I have added the below code to allow me to add a
new record. When the user clicks the AddButton a blank item is
displayed in the combobox, the users cursor is placed into the
combobox, and when they finish typing and press enter my intention is
to add a new record to the table. I have not added all the updating
and error checking code as of yet. Whenever the user presses enter,
then the call to EndEdit() produces an exception: "Column 'ProjectName'
does not allow
nulls."

I can fix this by binding the combobox Text property to the
ProjectNames column. However, I get a new exception whenever selecting
items in the combo box:
"Column 'ProjectName' is constrained to be unique. Value
'SomeProjectName' is already present."

It appears when I select a different item in the combobox, then it is
trying to use the new text value to update the previously select
record. Since my column requires unique values, then the update is not
possible.

The intention of selecting items in the combobox is that this effects
what is displayed by the seperate datagrid, which shows data in a table
on the many side of a many-to-one relationship with the ProjectNames
table by binding to the FK relationship.

So either:

I need to implement a replacement for the text binding behavior when
adding new items and updating items, so that I can remove the text
binding.

OR

I need to make sure when I select a different item, the selected record
is chosen, rather than the combobox thinking I'm trying to update the
current record with a new value selected from the combobox.

OR

Anything else someone might suggest.

private void addButton_Click(object sender, EventArgs e)
{
projectNamesBindingSource.AddNew();
projectNameComboBox.Focus();
}

private void projectNameComboBox_KeyUp(object sender,
KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
projectNamesBindingSource.EndEdit();
}
}

Jun 20 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
The SelectionChangeCommitted event for the combobox occurs before the
exception, so I guess I need to put into that event handler something
that will go ahead and move to the selected record. That way when the
databound Text property tries to write to the current record, then it
will write to the one it already belongs to.

I'm not really sure how to do this though.
sh******@cs.fsu.edu wrote:
I have a combobox that is very much like the one found in the RSS
project here:
http://msdn.microsoft.com/vstudio/ex...harp/learning/

My projectNameComboBox basically is filled with a list of values from a
table, and as the user selects values, a datagrid displays related
records from another table because it is bound via FK relationship.

My table:
/****** Object: Table [dbo].[ProjectNames] Script Date: 06/19/2006
19:46:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ProjectNames](
[ProjectName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL,
CONSTRAINT [PK_ProjectNames] PRIMARY KEY CLUSTERED
(
[ProjectName] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
I have the combobox bound to projectNamesBindSource, and projectNames
is the single column of names of projects. Display Member and Value
Member are both bound to ProjectName, which is the column in the
ProjectNames table. I have added the below code to allow me to add a
new record. When the user clicks the AddButton a blank item is
displayed in the combobox, the users cursor is placed into the
combobox, and when they finish typing and press enter my intention is
to add a new record to the table. I have not added all the updating
and error checking code as of yet. Whenever the user presses enter,
then the call to EndEdit() produces an exception: "Column 'ProjectName'
does not allow
nulls."

I can fix this by binding the combobox Text property to the
ProjectNames column. However, I get a new exception whenever selecting
items in the combo box:
"Column 'ProjectName' is constrained to be unique. Value
'SomeProjectName' is already present."

It appears when I select a different item in the combobox, then it is
trying to use the new text value to update the previously select
record. Since my column requires unique values, then the update is not
possible.

The intention of selecting items in the combobox is that this effects
what is displayed by the seperate datagrid, which shows data in a table
on the many side of a many-to-one relationship with the ProjectNames
table by binding to the FK relationship.

So either:

I need to implement a replacement for the text binding behavior when
adding new items and updating items, so that I can remove the text
binding.

OR

I need to make sure when I select a different item, the selected record
is chosen, rather than the combobox thinking I'm trying to update the
current record with a new value selected from the combobox.

OR

Anything else someone might suggest.

private void addButton_Click(object sender, EventArgs e)
{
projectNamesBindingSource.AddNew();
projectNameComboBox.Focus();
}

private void projectNameComboBox_KeyUp(object sender,
KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
projectNamesBindingSource.EndEdit();
}
}


Jun 20 '06 #2

P: n/a
Well enough shooting around in the dark and I finally hit the solution.
Added this to the SelectionChangeCommitted handler.

projectNamesBindingSource.Position = projectNameComboBox.SelectedIndex;
sh******@cs.fsu.edu wrote:
The SelectionChangeCommitted event for the combobox occurs before the
exception, so I guess I need to put into that event handler something
that will go ahead and move to the selected record. That way when the
databound Text property tries to write to the current record, then it
will write to the one it already belongs to.

I'm not really sure how to do this though.
sh******@cs.fsu.edu wrote:
I have a combobox that is very much like the one found in the RSS
project here:
http://msdn.microsoft.com/vstudio/ex...harp/learning/

My projectNameComboBox basically is filled with a list of values from a
table, and as the user selects values, a datagrid displays related
records from another table because it is bound via FK relationship.

My table:
/****** Object: Table [dbo].[ProjectNames] Script Date: 06/19/2006
19:46:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ProjectNames](
[ProjectName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL,
CONSTRAINT [PK_ProjectNames] PRIMARY KEY CLUSTERED
(
[ProjectName] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
I have the combobox bound to projectNamesBindSource, and projectNames
is the single column of names of projects. Display Member and Value
Member are both bound to ProjectName, which is the column in the
ProjectNames table. I have added the below code to allow me to add a
new record. When the user clicks the AddButton a blank item is
displayed in the combobox, the users cursor is placed into the
combobox, and when they finish typing and press enter my intention is
to add a new record to the table. I have not added all the updating
and error checking code as of yet. Whenever the user presses enter,
then the call to EndEdit() produces an exception: "Column 'ProjectName'
does not allow
nulls."

I can fix this by binding the combobox Text property to the
ProjectNames column. However, I get a new exception whenever selecting
items in the combo box:
"Column 'ProjectName' is constrained to be unique. Value
'SomeProjectName' is already present."

It appears when I select a different item in the combobox, then it is
trying to use the new text value to update the previously select
record. Since my column requires unique values, then the update is not
possible.

The intention of selecting items in the combobox is that this effects
what is displayed by the seperate datagrid, which shows data in a table
on the many side of a many-to-one relationship with the ProjectNames
table by binding to the FK relationship.

So either:

I need to implement a replacement for the text binding behavior when
adding new items and updating items, so that I can remove the text
binding.

OR

I need to make sure when I select a different item, the selected record
is chosen, rather than the combobox thinking I'm trying to update the
current record with a new value selected from the combobox.

OR

Anything else someone might suggest.

private void addButton_Click(object sender, EventArgs e)
{
projectNamesBindingSource.AddNew();
projectNameComboBox.Focus();
}

private void projectNameComboBox_KeyUp(object sender,
KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
projectNamesBindingSource.EndEdit();
}
}


Jun 20 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.