PC Review


Reply
Thread Tools Rate Thread

DataGridView ComboBox column with databound item list

 
 
sklett
Guest
Posts: n/a
 
      1st Sep 2006
I'm changing from a DataGrid to a DataGridView and have run across a
problem. The items that are bound to the DataGrid have an int Property that
represents a primary key of a lookup table in my database. For example:

table JobTypes
1 | Manager
2 | Controller
3 | Supervisor

table Employee
2 | JobTypeID
(other fields)

I want to display the JobType name in the column of the datagridview, then
when a user clicks to edit the type I want to display a ComboBox that lists
all the records from the JobType table. When the user selects an item, I
want to store it's ID in the bound Employee object in the JobTypeID field.

I hope that makes sense. The part that I'm not clear on is how to bind the
combobox column to a separate datasource and have the ComboBox column select
the item that matches the value of JobTypeID.

Has anyone done this? Seems like it would be a common task but I haven't
any examples showing how to accomplish this.

Thanks for reading,
Steve


 
Reply With Quote
 
 
 
 
sklett
Guest
Posts: n/a
 
      1st Sep 2006
I've created a binding source and specified it in the DataGridView combo
column as the data source.
Now I can't specify the ValueMember and DisplayMember properties. I can
type in them, but when I move to the next property they are erased.

The DataSource for the new BindingSource is a simple DataTable.

wow... that modal dialog that the DataGridView throws up over and over and
over could drive a man crazy :0(



"sklett" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I'm changing from a DataGrid to a DataGridView and have run across a
> problem. The items that are bound to the DataGrid have an int Property
> that represents a primary key of a lookup table in my database. For
> example:
>
> table JobTypes
> 1 | Manager
> 2 | Controller
> 3 | Supervisor
>
> table Employee
> 2 | JobTypeID
> (other fields)
>
> I want to display the JobType name in the column of the datagridview, then
> when a user clicks to edit the type I want to display a ComboBox that
> lists all the records from the JobType table. When the user selects an
> item, I want to store it's ID in the bound Employee object in the
> JobTypeID field.
>
> I hope that makes sense. The part that I'm not clear on is how to bind
> the combobox column to a separate datasource and have the ComboBox column
> select the item that matches the value of JobTypeID.
>
> Has anyone done this? Seems like it would be a common task but I haven't
> any examples showing how to accomplish this.
>
> Thanks for reading,
> Steve
>



 
Reply With Quote
 
Brian Kelly
Guest
Posts: n/a
 
      2nd Sep 2006
You need to bind the JobTypes datatable to your columns datasource.
Then set the DisplayMember and ValueMember properties. This will allow
Manager, Controller, etc to be displayed while their data
representation is 1, 2, etc... You then need to set the columns
datapropertyname to the "JobTypeID" column in the datagridview
datasource.

Put this in the Form_Load section of a form with a datagridview named
dataGridView1 for more clarity

dataGridView1.AutoGenerateColumns = false;

DataTable tableSource = new DataTable("tableSource");
tableSource.Columns.AddRange(new DataColumn[] {
new DataColumn("id"),
new DataColumn("job") });
tableSource.Rows.Add(1, "manager");
tableSource.Rows.Add(2, "supervisor");
tableSource.Rows.Add(3, "cashier");
tableGrid = new DataTable("tableGrid");
tableGrid.Columns.Add("jobid");
tableGrid.Rows.Add(2);

dataGridView1.DataSource = tableGrid;

DataGridViewComboBoxColumn col = new
DataGridViewComboBoxColumn();
col.DataSource = tableSource;
col.DisplayMember = "job";
col.ValueMember = "id";
col.DataPropertyName = "jobid";
dataGridView1.Columns.Add(col);

sklett wrote:
> I've created a binding source and specified it in the DataGridView combo
> column as the data source.
> Now I can't specify the ValueMember and DisplayMember properties. I can
> type in them, but when I move to the next property they are erased.
>
> The DataSource for the new BindingSource is a simple DataTable.
>
> wow... that modal dialog that the DataGridView throws up over and over and
> over could drive a man crazy :0(
>
>
>
> "sklett" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > I'm changing from a DataGrid to a DataGridView and have run across a
> > problem. The items that are bound to the DataGrid have an int Property
> > that represents a primary key of a lookup table in my database. For
> > example:
> >
> > table JobTypes
> > 1 | Manager
> > 2 | Controller
> > 3 | Supervisor
> >
> > table Employee
> > 2 | JobTypeID
> > (other fields)
> >
> > I want to display the JobType name in the column of the datagridview, then
> > when a user clicks to edit the type I want to display a ComboBox that
> > lists all the records from the JobType table. When the user selects an
> > item, I want to store it's ID in the bound Employee object in the
> > JobTypeID field.
> >
> > I hope that makes sense. The part that I'm not clear on is how to bind
> > the combobox column to a separate datasource and have the ComboBox column
> > select the item that matches the value of JobTypeID.
> >
> > Has anyone done this? Seems like it would be a common task but I haven't
> > any examples showing how to accomplish this.
> >
> > Thanks for reading,
> > Steve
> >


 
Reply With Quote
 
sklett
Guest
Posts: n/a
 
      5th Sep 2006
Hi Brian,

Thank you for the example code!
I was doing essentially the same thing, except I was using the designer. I
changed over to setting things up with code using my data sources and
property names and I'm still getting the same error:
"DataGridViewComboBoxCell value is not valid."

When I cut and paste your code, it works fine. I've checked the values of
my data source(s) and they seem correct.

So I looked a little closer and realized what the problem is; I have
mismatched types, my ValueMember is a byte and my DataPropertyName is an
int.

This is one of those times where a more detailed error message would be
great. It's also one of those times when I realize I've done something
stupid ;0)

"Brian Kelly" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> You need to bind the JobTypes datatable to your columns datasource.
> Then set the DisplayMember and ValueMember properties. This will allow
> Manager, Controller, etc to be displayed while their data
> representation is 1, 2, etc... You then need to set the columns
> datapropertyname to the "JobTypeID" column in the datagridview
> datasource.
>
> Put this in the Form_Load section of a form with a datagridview named
> dataGridView1 for more clarity
>
> dataGridView1.AutoGenerateColumns = false;
>
> DataTable tableSource = new DataTable("tableSource");
> tableSource.Columns.AddRange(new DataColumn[] {
> new DataColumn("id"),
> new DataColumn("job") });
> tableSource.Rows.Add(1, "manager");
> tableSource.Rows.Add(2, "supervisor");
> tableSource.Rows.Add(3, "cashier");
> tableGrid = new DataTable("tableGrid");
> tableGrid.Columns.Add("jobid");
> tableGrid.Rows.Add(2);
>
> dataGridView1.DataSource = tableGrid;
>
> DataGridViewComboBoxColumn col = new
> DataGridViewComboBoxColumn();
> col.DataSource = tableSource;
> col.DisplayMember = "job";
> col.ValueMember = "id";
> col.DataPropertyName = "jobid";
> dataGridView1.Columns.Add(col);
>
> sklett wrote:
>> I've created a binding source and specified it in the DataGridView combo
>> column as the data source.
>> Now I can't specify the ValueMember and DisplayMember properties. I can
>> type in them, but when I move to the next property they are erased.
>>
>> The DataSource for the new BindingSource is a simple DataTable.
>>
>> wow... that modal dialog that the DataGridView throws up over and over
>> and
>> over could drive a man crazy :0(
>>
>>
>>
>> "sklett" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > I'm changing from a DataGrid to a DataGridView and have run across a
>> > problem. The items that are bound to the DataGrid have an int Property
>> > that represents a primary key of a lookup table in my database. For
>> > example:
>> >
>> > table JobTypes
>> > 1 | Manager
>> > 2 | Controller
>> > 3 | Supervisor
>> >
>> > table Employee
>> > 2 | JobTypeID
>> > (other fields)
>> >
>> > I want to display the JobType name in the column of the datagridview,
>> > then
>> > when a user clicks to edit the type I want to display a ComboBox that
>> > lists all the records from the JobType table. When the user selects an
>> > item, I want to store it's ID in the bound Employee object in the
>> > JobTypeID field.
>> >
>> > I hope that makes sense. The part that I'm not clear on is how to bind
>> > the combobox column to a separate datasource and have the ComboBox
>> > column
>> > select the item that matches the value of JobTypeID.
>> >
>> > Has anyone done this? Seems like it would be a common task but I
>> > haven't
>> > any examples showing how to accomplish this.
>> >
>> > Thanks for reading,
>> > Steve
>> >

>



 
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
Re: Select combobox item by invisible column when Limit To List = No Douglas J. Steele Microsoft Access Form Coding 1 10th Sep 2009 12:22 PM
RE: Select combobox item by invisible column when Limit To List = No Jack Leach Microsoft Access Form Coding 1 9th Sep 2009 06:13 PM
Add blank item in databound ComboBox Benny Microsoft C# .NET 3 19th Jan 2007 10:50 PM
DataGridView with databound combobox =?Utf-8?B?SmFrb2IgTGl0aG5lcg==?= Microsoft ASP .NET 9 2nd Apr 2006 04:46 PM
How do I add a blank item to a combobox that is databound to a dataset? Johann Blake Microsoft C# .NET 6 22nd Sep 2005 05:54 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:46 AM.