DataGridView with ComboBox. Need help
-
I have been wracking my brain for hours on this problem. I have a DataGridView object that has a DataGridViewComboBoxColumn inside of it. The combo box displays fine and is populated from an Access database. The column has the Primary key mapped to the ValueMember variable and the column 'Type' is mapped to DisplayMember. What I need is for each of these combo boxes to have their selected index (I think it is the Value property under DataGridViewComboBoxCell, but I'm pulling at straws at this point) be populated from another table. This took me about 6 seconds in Access. My problem is I cannot even find a way to access the Value/SelectedIndex of the Combobox once I place it in the DataGridView. If anyone could help me out I'd greatly appreciate it. Here is the code:
//Connection string to DB. OleDbConnection dbCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\"" + "NGDemoSetup.mdb" + "\\""); //Dataset for majority of data DataSet ds = new DataSet(); //Dataset for the combobox DataSet cboDs = new DataSet(); //Create a DGVComboBoxColumn to hold the Types we have in the ADDRESSTYPES Table of the DB. DataGridViewComboBoxColumn cboCol = new DataGridViewComboBoxColumn(); //Open the DB. dbCon.Open(); //Grab all the data we need from the Location Info table OleDbDataAdapter da = new OleDbDataAdapter("SELECT AddrType,isRange,Value1,Value2 FROM LOCATIONINFO", dbCon); //Grab all the columns from ADDRESSTYPES (ID and Types). OleDbDataAdapter cboDa = new OleDbDataAdapter("SELECT \* FROM ADDRESSTYPES", dbCon); da.Fill(ds, "LocationInfo"); cboDa.Fill(cboDs, "AddressTypes"); cboCol.Width = 100; cboCol.DataSource = cboDs.Tables\[0\]; cboCol.ValueMember = "ID"; cboCol.DisplayMember = "Type"; DataTable dt = ds.Tables\["LocationInfo"\]; grdAddressInfo.DataSource = dt; /\* \* Somehow make the ComboBox Column display the correct Dropdown object \* Based upon the AddrType field. \*/ //Column formatting grdAddressInfo.Columns.Insert(0, cboCol); grdAddressInfo.Columns\[0\].HeaderText = "Address Type"; grdAddressInfo.Columns\[1\].HeaderText = "Ranged Value"; grdAddressInfo.Columns\[2\].HeaderText = "From"; grdAddressInfo.Columns\[3\].HeaderText = "To"; dbCon.Close();
-
I have been wracking my brain for hours on this problem. I have a DataGridView object that has a DataGridViewComboBoxColumn inside of it. The combo box displays fine and is populated from an Access database. The column has the Primary key mapped to the ValueMember variable and the column 'Type' is mapped to DisplayMember. What I need is for each of these combo boxes to have their selected index (I think it is the Value property under DataGridViewComboBoxCell, but I'm pulling at straws at this point) be populated from another table. This took me about 6 seconds in Access. My problem is I cannot even find a way to access the Value/SelectedIndex of the Combobox once I place it in the DataGridView. If anyone could help me out I'd greatly appreciate it. Here is the code:
//Connection string to DB. OleDbConnection dbCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\"" + "NGDemoSetup.mdb" + "\\""); //Dataset for majority of data DataSet ds = new DataSet(); //Dataset for the combobox DataSet cboDs = new DataSet(); //Create a DGVComboBoxColumn to hold the Types we have in the ADDRESSTYPES Table of the DB. DataGridViewComboBoxColumn cboCol = new DataGridViewComboBoxColumn(); //Open the DB. dbCon.Open(); //Grab all the data we need from the Location Info table OleDbDataAdapter da = new OleDbDataAdapter("SELECT AddrType,isRange,Value1,Value2 FROM LOCATIONINFO", dbCon); //Grab all the columns from ADDRESSTYPES (ID and Types). OleDbDataAdapter cboDa = new OleDbDataAdapter("SELECT \* FROM ADDRESSTYPES", dbCon); da.Fill(ds, "LocationInfo"); cboDa.Fill(cboDs, "AddressTypes"); cboCol.Width = 100; cboCol.DataSource = cboDs.Tables\[0\]; cboCol.ValueMember = "ID"; cboCol.DisplayMember = "Type"; DataTable dt = ds.Tables\["LocationInfo"\]; grdAddressInfo.DataSource = dt; /\* \* Somehow make the ComboBox Column display the correct Dropdown object \* Based upon the AddrType field. \*/ //Column formatting grdAddressInfo.Columns.Insert(0, cboCol); grdAddressInfo.Columns\[0\].HeaderText = "Address Type"; grdAddressInfo.Columns\[1\].HeaderText = "Ranged Value"; grdAddressInfo.Columns\[2\].HeaderText = "From"; grdAddressInfo.Columns\[3\].HeaderText = "To"; dbCon.Close();
You can create objects of the items and then add the list of objects to the combo box.This way you can access the selected object from the datagridview cell.