Windows Develop Bookmark and Share   
 index > Windows Forms General > Problem in Datagrid Combo Box in Win Forms
 

Problem in Datagrid Combo Box in Win Forms

Hi all,

I have one problem with DataGridViewComboboxColumn in Windows forms. Here the scenario is i have one datagrid and i am adding two Combo Box  in two columns to that datagrid dynamically(Run Time). It is adding two combo box. In those combo box there are 5 values. Here i have to find out the event when i selecting the value in combo box. What is the event to find values changed in combo box. I tried with cell click event. But its not working properly. I cant mention combo box event as it is adding dynamically. What is the solution for this?  Thank you..
Ganapatisb  Wednesday, October 07, 2009 10:32 AM
Hi,

To find out the selected values of combo box , must register the DataGridViewComboBoxColumn in Editing Mode and then add the ComBox selected index event to access the values.

1. CellEnter event.

 private void dGridAddDocuments_CellEnter(object sender, DataGridViewCellEventArgs e)
        {          
//For example column 1 and 2 are comboBox columns
            if ((e.ColumnIndex == 1 ||  e.ColumnIndex == 2) && dGridAddDocuments.SelectedRows.Count > 0)
            {
                try
                {
                    dGridAddDocuments.BeginEdit(true);
                    DataGridViewCell dc = dGridAddDocuments.SelectedRows[0].Cells[e.ColumnIndex];
                    dGridAddDocuments.ReadOnly = false;
                    dGridAddDocuments.CurrentCell = dc;
                    dGridAddDocuments.BeginEdit(true);
                }
                catch (System.Exception ex)
                {
                     throw ex;
                }
            }
        }
2. EditingControlShowing Event.

private void dGridAddDocuments_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {           
            if (dGridAddDocuments.CurrentCell.ColumnIndex == 1 ||
               dGridAddDocuments.CurrentCell.ColumnIndex == 2)
            {
                // Check box column   
                ComboBox cb = e.Control as ComboBox;
                if (cb != null)
                {
                    //// first remove event handler to keep from attaching multiple:
                    cb.SelectedIndexChanged -= new EventHandler(combo_SelectedIndexChanged);
                    //cb.SelectionChangeCommitted -= new EventHandler(combo_SelectionChangeCommitted);
                    //// now attach the event handler
                    cb.SelectedIndexChanged += new EventHandler(combo_SelectedIndexChanged);
                    //cb.SelectionChangeCommitted += new EventHandler(combo_SelectionChangeCommitted);
                }
            }
        }

3.EndEdit Event of DagaGricView.

 private void dGridAddDocuments_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {         
            dGridAddDocuments.ReadOnly = true;
            dGridAddDocuments.BeginEdit(false);
        }


4. Combo Box selected Index Event. 

private void combo_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataGridViewComboBoxEditingControl combo = (DataGridViewComboBoxEditingControl)sender;

            if (combo.SelectedItem == null)
                return;
            switch (dGridAddDocuments.CurrentCell.ColumnIndex)
            {
                   case 1:
                       string strValue1 = combo.SelectedItem.ToString();
                        break;
                   case 2:
                       string strValue2 = combo.SelectedItem.ToString();
                        break;
            }
       }
Any Further information let me know. 

Malik M.Shahid  Wednesday, October 07, 2009 11:40 AM
Hi,

To find out the selected values of combo box , must register the DataGridViewComboBoxColumn in Editing Mode and then add the ComBox selected index event to access the values.

1. CellEnter event.

 private void dGridAddDocuments_CellEnter(object sender, DataGridViewCellEventArgs e)
        {          
//For example column 1 and 2 are comboBox columns
            if ((e.ColumnIndex == 1 ||  e.ColumnIndex == 2) && dGridAddDocuments.SelectedRows.Count > 0)
            {
                try
                {
                    dGridAddDocuments.BeginEdit(true);
                    DataGridViewCell dc = dGridAddDocuments.SelectedRows[0].Cells[e.ColumnIndex];
                    dGridAddDocuments.ReadOnly = false;
                    dGridAddDocuments.CurrentCell = dc;
                    dGridAddDocuments.BeginEdit(true);
                }
                catch (System.Exception ex)
                {
                     throw ex;
                }
            }
        }
2. EditingControlShowing Event.

private void dGridAddDocuments_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {           
            if (dGridAddDocuments.CurrentCell.ColumnIndex == 1 ||
               dGridAddDocuments.CurrentCell.ColumnIndex == 2)
            {
                // Check box column   
                ComboBox cb = e.Control as ComboBox;
                if (cb != null)
                {
                    //// first remove event handler to keep from attaching multiple:
                    cb.SelectedIndexChanged -= new EventHandler(combo_SelectedIndexChanged);
                    //cb.SelectionChangeCommitted -= new EventHandler(combo_SelectionChangeCommitted);
                    //// now attach the event handler
                    cb.SelectedIndexChanged += new EventHandler(combo_SelectedIndexChanged);
                    //cb.SelectionChangeCommitted += new EventHandler(combo_SelectionChangeCommitted);
                }
            }
        }

3.EndEdit Event of DagaGricView.

 private void dGridAddDocuments_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {         
            dGridAddDocuments.ReadOnly = true;
            dGridAddDocuments.BeginEdit(false);
        }


4. Combo Box selected Index Event. 

private void combo_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataGridViewComboBoxEditingControl combo = (DataGridViewComboBoxEditingControl)sender;

            if (combo.SelectedItem == null)
                return;
            switch (dGridAddDocuments.CurrentCell.ColumnIndex)
            {
                   case 1:
                       string strValue1 = combo.SelectedItem.ToString();
                        break;
                   case 2:
                       string strValue2 = combo.SelectedItem.ToString();
                        break;
            }
       }
Any Further information let me know. 

Malik M.Shahid  Wednesday, October 07, 2009 11:40 AM
Thank you... :)
Ganapatisb  Thursday, October 08, 2009 5:34 AM

You can use google to search for other answers

Custom Search

More Threads

• Capricious ToolTip
• my driver sounds is lost from my pc
• Working with Outlook E-mails | Creating a New Message
• Customising the TextBox
• share violation access on files in windows
• registers set by gpedit.msc
• Specific Element Opacity
• Drag and Drop registration generates a security exception
• How to display data from database one after another in Timer tick event
• How Can I Make a DropShadow?