Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am not much familiar with winform applications.
I have a datagrid view in which I am adding a combobox column.
I want to set the value of a combobox for each row.

This is what I am doing:
C#
private void BindGrid(DataTable Table)
        {
            dgvSchemaMapped.AutoGenerateColumns = false;
            dgvSchemaMapped.Columns[0].Name = "COLUMN_NAME";
            dgvSchemaMapped.Columns[0].HeaderText = "Column";
            dgvSchemaMapped.Columns[0].DataPropertyName = "COLUMN_NAME";

            dgvSchemaMapped.Columns[1].Name = "ALIAS_COLUMN";
            dgvSchemaMapped.Columns[1].HeaderText = "Alias Column";
            dgvSchemaMapped.Columns[1].DataPropertyName = "ALIAS_COLUMN";

            dgvSchemaMapped.Columns[2].Name = "TYPE_NAME";
            dgvSchemaMapped.Columns[2].HeaderText = "Data Type";
            dgvSchemaMapped.Columns[2].DataPropertyName = "TYPE_NAME";

            dgvSchemaMapped.DataSource = Table;

            //Add combobox column to gird
            DataGridViewComboBoxColumn AliasDataTypeCombo = new DataGridViewComboBoxColumn();
            FillDataTypeCombo(AliasDataTypeCombo);
            dgvSchemaMapped.Columns.Add(AliasDataTypeCombo);

            // Set default value to aliasDataType combobox
            SetDefaultDataType();
        }


C#
private void SetDefaultDataType()
        {
            for (int i = 0; i < dgvSchemaMapped.Rows.Count - 1; i++)
            {
                string dataType = dgvSchemaMapped.Rows[i].Cells["TYPE_NAME"].Value.ToString();
                switch (dataType.ToLower())
                {
                    case "int":
                    case "tinyint":
                    case "smallint":
                        (dgvSchemaMapped.Rows[i].Cells[3]).Value = ((KeyValuePair<string, int>)(((dgvSchemaMapped.Rows[i].Cells[3] as DataGridViewComboBoxCell).Items[3]))).Value;
                        break;
                    case "char":
                    case "nchar":
                    case "varchar":
                    case "nvarchar":
                        (dgvSchemaMapped.Rows[i].Cells[3]).Value = ((KeyValuePair<string, int>)((dgvSchemaMapped.Rows[i].Cells[3] as DataGridViewComboBoxCell).Items[18])).Value;
                        break;
                    case "real":
                    case "decimal":
                        (dgvSchemaMapped.Rows[i].Cells[3]).Value = ((KeyValuePair<string, int>)((dgvSchemaMapped.Rows[i].Cells[3] as DataGridViewComboBoxCell).Items[12])).Value;
                        break;
                    case "datetime":
                        (dgvSchemaMapped.Rows[i].Cells[3]).Value = ((KeyValuePair<string, int>)((dgvSchemaMapped.Rows[i].Cells[3] as DataGridViewComboBoxCell).Items[22])).Value;
                        break;
                }
            }
        }

C#
private void FillDataTypeCombo(DataGridViewComboBoxColumn AliasDataTypeCombo)
       {
           Dictionary<string, int> lstDataTypes = new Dictionary<string, int>();

           GetBuilInDataTypes(lstDataTypes);

           AliasDataTypeCombo.DataSource = new BindingSource(lstDataTypes, null);
           AliasDataTypeCombo.ValueMember = "value";
           AliasDataTypeCombo.DisplayMember = "key";
       }


The comboboxes do have values in them when grid is bound. But I am not able to set the default value to each combobox.
What am I doing wrong?

Any help appreciated.
Thanks,
Lok.

What I have tried:

I tried casting the grid cell to combobox cell and assign value to it.
But this gives me no result.
Posted
Updated 12-Jul-17 1:00am

If the BindingSource does not work, try like this:
C#
AliasDataTypeCombo.Items.AddRange("One", "Two", "Three", "Four");
Could also be a scope problem as you assign the BindingSource directly, try with a public BindingSource variable.
 
Share this answer
 
v2
Comments
Lokesh Zende 12-Jul-17 1:45am    
I can see items in each combobox in a column. So I don't think it has anything to do with BindingSource. Or does it?
RickZeeland 12-Jul-17 2:15am    
I should say simply try it out, it is easy to implement ...
Lokesh Zende 12-Jul-17 5:16am    
Thanks for suggestion. I tried this also. Makes no difference
If the comboboxes are blank after the SetDefaultType() method has executed then the most likely reason is that either an incorrect or no value was assigned to each combobox. The question becomes how could that happen?

Your code for getting the default values from the Items collection seems to be correct so I have these simple questions (don't be offended)

1) Is this loop condition correct as it excludes the last row in the DGV?
C#
for (int i = 0; i < dgvSchemaMapped.Rows.Count - 1; i++)

2) Are you using the DataGridView.DataError event. It is almost essential during debugging as the DGV has a nasty habit of swallowing exceptions and the event is often the only way to reveal a problem with the data. For example
comboboxCell.Value = SomethingNotInTheItemsList;
is a data error and will give a blank cell.

3) In the switch is dataType.ToLower() a valid value. I'm asking if any of the switch sections (cases) execute. Possibly add a default case with an exception throw statement to highlight an unexpected values in dataType.

Two comments about the code

That complex index/cast/index/cast/assign one liner is really difficult to understand and splitting it up into individual parts will aid debugging. Only the last two lines need to be inside the switch.
C#
DataGridViewRow row = grid.Rows[i];

DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)row.Cells[3];
DataGridViewComboBoxCell.ObjectCollection items = cell.Items;

KVP defaultItem = (KVP)items[18];  // KVP is an alias for the full KeyValuePair<>
cell.Value = defaultItem.Value;

Why a Dictionary for the combobox items as the BindingSource has to do an implicit conversion to an IBindingList.

Alan.
 
Share this answer
 

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