Click here to Skip to main content
15,878,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

i have a problem SETTING the DatagridviewComboBoxColumn selectedValue.

I have a grid that has 2 DatagridviewComboBoxColum.

please see the code below.
C#
DataTable datatable = new DataTable(DataSourceTable);
                     datatable = oDbConnection.MYSQLExecuteNonQuery("SELECT  * FROM "+ DataSourceTable , datatable);
                     DataGridViewComboBoxColumn oCol = new DataGridViewComboBoxColumn();

                     oCol.Name = ColumnName;
                     oCol.HeaderText = HeaderText;
                     oCol.DisplayMember = DisplayMember;
                     oCol.ValueMember = ValueMember;
                     oCol.DataPropertyName = ValueMember;
                     oCol.DataSource = datatable;
                     DgView.Columns.Add(oCol);



where ValueMember = "ProjectID"
and DisplayMember = "ProjectDescrption"

i dont have probelem GETing the value of the DatagridviewComboBoxColum

the problem is when i set the DatagridviewComboBoxColum value when i load the data in the datagridview which has ComboBox from the database.

instead displaying of DISPLAYMEMBER data, the comboBox Displying VALUEMEMBER.

thanks.
Posted
Updated 11-Jan-19 19:24pm

If you mean the the combobox is showing the value member data (data from Id column) instead of data from ProjectDescription column, then use the debugger to check that your variables (DisplayMember and ValueMember) contain correct column names.

If you mean that the display member data isn't showing the data at all, check that the column name is spelled correctly. Should the
C#
DisplayMember = "ProjectDescrption"

be
C#
DisplayMember = "ProjectDescription"
 
Share this answer
 
Hi,

The spelling is correct. what i'm trying to figure is how to SET the SELECTEDVALUE of the DatagridViewComboBoxColumn.


as per MSDN, the DatagridViewComboBoxColumn has no property of SELECTEDVALUE but VALUE.
 
Share this answer
 
in grid view combobox cell property value act as both selected value and selected text.
if you want to set grid combo selected value it only accepts integer type. if assigned value is string it automatically sets selected text of the grid combo.

eg:

dgv_Sales.Rows[i].Cells["grid_cmbUnit"].Value = 1 // this line of code sets selectedvalue


dgv_Sales.Rows[i].Cells["grid_cmbUnit"].Value = "KG" //this line of code sets selectedtext
 
Share this answer
 
Comments
Member 2244190 29-Feb-24 9:50am    
When I use the .Value setting it to a number it is showing the actual number instead of the Text associated to that number.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900