If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Retrieving index of combobox in datagridview for update to DB

Most of my experience in in VB6... I am trying to convert to VB.NET. I have visual studio 2013.

I have a datagridview with multiple comboboxes. The idea if for the user to select values for all combos and then click the "Save" button to update the database.

I am only storing the numeric equivalent which refers to the index. I cannot figure out how to retrieve the index of the selection in each cell. I was thinking somehow I could use ENUM but can't figure out how to use that either.

I don't have time right now to clean it up but you should be able to figure it out. From this added event you could store the index in a Form level variable for use later. Instead of this you could create a Dictionary to hold the ComboBox values and corresponding index then look the index up using the combobox value just before you save the data.

One thing I would like to say is I'm not sure what your doing is a good idea. What happens if in the future for some unforeseen reason you have to add or remove an item from the combobox list, that would make all the stored index numbers wrong.

Re: Retrieving index of combobox in datagridview for update to DB

You appear to be going about this all wrong. Most of this should happen automatically, if you have set everything up correctly.

You should start with two related tables in your database. Let's take a simple example like gender. You would have a Gender table with GenderId and Name columns and then you would have a Person table with a GenderId column that is a foreign key to the Gender table.

You can then query your database and populate a DataTable with your Person data. If you bind that DataTable to a DataGridView, you will end up with a text box column with the header text "GenderId" and you will see the numerical GenderId values in the column. What you can do instead is add a combo box to your grid and bind it to a second DataTable containing the data from the Gender table. If you set up the bindings properly, the user will then see the values from the Name column of the Gender table in the grid. The appropriate Name value will be automatically selected for each Person record. When the user selects a Gender Name value in a combo box, the corresponding GenderId will automatically be pushed to the Person record in that row. You don't have to do anything.

What is actually happening is that the data-binding system is moving data back and forth between the Value property of the cell, which contains a GenderId from the Person record, and the SelectedValue property of the ComboBox control, which contains the GenderId from a Gender record. When the grid is populated with Person records, the appropriate Gender record is selected based on the corresponding GenderId values and the Name displayed. When the user starts editing a cell, a ComboBox control is created and embedded in the cell. The Value of the cell is assigned to the SelectedValue of the control and the corresponding Name is displayed. If the user changes the selection then the SelectedValue property changes. When the user ends the editing session, the ComboBox control is removed from the cell and its SelectedValue property is assigned to the Value of the cell.

After all that, all you have to do is save the DataTable bound to the grid and all the correct GenderId values will be saved to the database without you having to write ANY code to interact with ANY ComboBox control. You might like to follow the CodeBank link in my signature below and check out my thread on Adding A ComboBox Column To A DataGridView.

Re: Retrieving index of combobox in datagridview for update to DB

Thank you Jmcilhinney... The concept just seems so foreign to me! I know I need to change the way I think about coding with .Net versus vb6, but it is difficult for me.

I will take a look at the link and give it a try.

A bit more info... The grid gets populated based on selections made. I attached a screen shot that shows what my form looks like before being populated. Also, the number of columns and the header name for the columns is dynamic based on the selections made.