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