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" <sk****@mddirect.comwrote in message
news:ON**************@TK2MSFTNGP04.phx.gbl...
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