PC Review


Reply
Thread Tools Rate Thread

Databound combobox, new and selected value confusion.

 
 
shumaker@cs.fsu.edu
Guest
Posts: n/a
 
      20th Jun 2006
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();
}
}

 
Reply With Quote
 
 
 
 
shumaker@cs.fsu.edu
Guest
Posts: n/a
 
      20th Jun 2006
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.


(E-Mail Removed) 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();
> }
> }


 
Reply With Quote
 
shumaker@cs.fsu.edu
Guest
Posts: n/a
 
      20th Jun 2006
Well enough shooting around in the dark and I finally hit the solution.
Added this to the SelectionChangeCommitted handler.

projectNamesBindingSource.Position = projectNameComboBox.SelectedIndex;


(E-Mail Removed) 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.
>
>
> (E-Mail Removed) 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();
> > }
> > }


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGridView with databound combobox =?Utf-8?B?SmFrb2IgTGl0aG5lcg==?= Microsoft ASP .NET 9 2nd Apr 2006 04:46 PM
Synch DataBound ComboBox John Smith Microsoft C# .NET 0 8th Jun 2005 11:46 PM
DataBound ComboBox Dave Microsoft Dot NET Framework Forms 4 10th Jun 2004 03:36 PM
Creating a databound ComboBox Matt Burland Microsoft ADO .NET 2 4th Nov 2003 02:04 PM
Databound combobox jan v Microsoft C# .NET 2 20th Aug 2003 08:19 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:09 PM.