Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridViewComboBox need ID that corresponds to Displayed value
 

DataGridViewComboBox need ID that corresponds to Displayed value

Hi All,

I have a DatagridView with ComboBox cells/columns which works fine but I really need to store the Database ID of each item in the ComboBox so that I can safely refer back to the DB based on the selection made by the user. I can't use the displayed value in the ComboBox to find the item in the DBas these values are not unique and can only be referenced safely using their respective Primary Key (the value I want to store in the Combobox but not show the user).

Basically what I want is a ComboBox that acts like a typical HTML combobox with a Value and a Dislay Member ie

<option Value="400">Rob</option>

<option Value="505">Bob</option>

<option Value="322">Luke</option>

NB the comboboxes are filled on a per row basis. Each row containsa uniqueclientand the dropdown box is filled with the names of the petseach client owns.

Regards

Rob

rcfearn  Monday, May 12, 2008 12:54 PM

Hi Rcfearn,

Well, based on my experience, I think the solution depends on the design of your database tables.

If you have defined “Pets�table with columns like following:

PetID

PetsName

ClientID or ClientName

You can find pets info without using Pet’s ID. Instead, it can be found by the combination of Pet’s Name and ClientID, since ClientID is unique in database and different pets of the same client must have different names too.

But, if you defined “Pets�table like this:

PetID

PetsName

I’m afraid it is difficult to get PetId under your situation.

I have to say that it’s not the best design, which may weaken the relation between “Pets�table and “Client�table.

If tables are defined like that, maybe you can get PetID like the following steps:

1. When you load data into datagridview, create another datatable which has the schema like the former design I listed above. Let’s call it Temp_Pet_Table

2. Each time user clicks the ComboBox column to choose a pet, generate a select command to find pet info in Temp_Pet_Table.

Then pet’s info could be found.

Hope it helps.

Please feel free to let me know how it your problem is going on. If there is any question, please feel free to rise, and I’ll try to give you solution as I can.

Best wishes,

Jun Wang

Jun Wang Tim  Friday, May 16, 2008 9:07 AM

Hi Rcfearn,

Well, based on my experience, I think the solution depends on the design of your database tables.

If you have defined “Pets�table with columns like following:

PetID

PetsName

ClientID or ClientName

You can find pets info without using Pet’s ID. Instead, it can be found by the combination of Pet’s Name and ClientID, since ClientID is unique in database and different pets of the same client must have different names too.

But, if you defined “Pets�table like this:

PetID

PetsName

I’m afraid it is difficult to get PetId under your situation.

I have to say that it’s not the best design, which may weaken the relation between “Pets�table and “Client�table.

If tables are defined like that, maybe you can get PetID like the following steps:

1. When you load data into datagridview, create another datatable which has the schema like the former design I listed above. Let’s call it Temp_Pet_Table

2. Each time user clicks the ComboBox column to choose a pet, generate a select command to find pet info in Temp_Pet_Table.

Then pet’s info could be found.

Hope it helps.

Please feel free to let me know how it your problem is going on. If there is any question, please feel free to rise, and I’ll try to give you solution as I can.

Best wishes,

Jun Wang

Jun Wang Tim  Friday, May 16, 2008 9:07 AM

Hi Rcfearn,

I’m going to mark my last reply as answer.

If there is still problem, please unmark it and provide more information, then I’ll try to give you solution as I can.

Best wishes,

Jun Wang

Jun Wang Tim  Friday, May 16, 2008 11:44 AM

You can use google to search for other answers

Custom Search

More Threads

• DataColumnChanging problems...
• Datagrid - Dynamic Bound Column -Column Names case Sensitive
• Binding Fails In One Table In a Dataset
• How to change the column name in datagrid?
• A way to change the values of a collection of selected (i.e. highlighted) checkboxcells in a DataGridViewCheckBoxColumn?
• Bindable UserControl
• help!!!
• Index out of Range in datagrid.select()
• problems when sql server replicated
• Custom insert, delete, update