Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridViewComboBoxColum on selectedIndexChanged
 

DataGridViewComboBoxColum on selectedIndexChanged

Hi,

I've been looking all over google and forums to find out how to do this but there is no quick and easy answer and a load of harder answers that are all different and people never post back to forums whether they actually worked or not.

I have a datagridview with 5 columns, The first column is a drop down list which specifies the type of "treatment" a seed is going to get, once this is selected the second column which is also a drop down list needs to be populated with treatments within the group selected. For example "Extraction" might be selected on the first drop down therefore options such as "AD - Air dried in building, SD - Sun Dried" etc are populated into the second drop down. They can then enter the other fields which are text boxes and add another row to the datagrid to add the next treatment. To make it perfect it would be nice for the treatment drop down list to have two columns, so they can type "AD" as the main column and it displays "AD Air Dried", Air Dried being the second column.

It seems as though there is a lot of people out there also frustrated with not being able to capture the event onselectedindexchanged and to change the information for that row. Is this to hard to acomplish in the .Net framework? I understand if it isn't possible, I would just appreciate any answers that give a good solution or answer. If not then I will find another way for the customer to enter the data. Also, can theDataGridViewComboBoxColum have more than one column?
I assume if this is all possible I will have to write a custom control? I'm trying not to spend to much time on this problem but don't want to leave the customer with something that could have been a lot better.

Thanks!


Rebecca


RebeccaAtDragonFly  Wednesday, August 12, 2009 8:53 PM
Hi Rebacca,
If you want selectedIndexChanged of datagridviewCombocolumn, thenYou can handle the cellEditingControlShowing event, add selectedIndexChanged event to the combobox, thus you can get the selected value. The following code demostrates how to do this:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    ComboBox cb = e.Control as ComboBox;
    if (cb != null)
    {
        // first remove event handler to keep from attaching multiple:
        cb.SelectedIndexChanged -= new EventHandler(cb_SelectedIndexChanged); 

        // now attach the event handler
        cb.SelectedIndexChanged += new   EventHandler(cb_SelectedIndexChanged);
    }

} 

void cb_SelectedIndexChanged(object sender, EventArgs e)
{ 
      ComboBox cb = sender as ComboBox;
MessageBox.Show(cb.selectedValue.toString());
}


if above not help you, please provider more details or code.
NareshG  Thursday, August 13, 2009 6:08 AM

Hi,
I hope it will help you

private void Form1_Load(object sender, EventArgs e)
        {
            AddColumns();
            dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
        }

        void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {

            if (dataGridView1.CurrentCell.ColumnIndex == 0)
            {
                ComboBox cb = e.Control as ComboBox;
                if (cb != null)
                {
                    cb.SelectedIndexChanged -= new EventHandler(cb_SelectedIndexChanged);
                    cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
                }
            }
            
        }

        void cb_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataGridViewComboBoxColumn cbo = dataGridView1.Columns[1] as DataGridViewComboBoxColumn;
            cbo.Items.AddRange("Plan1", "Plan2", "Plan3", "Plan4", "Plan5");
            
        }
        private void AddColumns()
        {
            DataGridViewComboBoxColumn column1 = new DataGridViewComboBoxColumn();
            column1.HeaderText = "Treatement";
            column1.Width = 90;
            column1.FlatStyle = FlatStyle.Flat;
            column1.Items.AddRange("Treatment1", "Treatment2", "Treatment3", 
"Treatment4", "Treatment5");         
        
            DataGridViewComboBoxColumn column2 = new DataGridViewComboBoxColumn();
            column2.HeaderText = "TreatementPlan";
            column2.Width = 90;
            column2.FlatStyle = FlatStyle.Flat;           
            DataGridViewTextBoxColumn column3 = new DataGridViewTextBoxColumn();
            column3.HeaderText = "Action1";
            column3.Width = 90;
            DataGridViewTextBoxColumn column4 = new DataGridViewTextBoxColumn();
            column4.HeaderText = "Action2";
            column4.Width = 90;
            dataGridView1.Columns.Add(column1);
            dataGridView1.Columns.Add(column2);
            dataGridView1.Columns.Add(column3);
            dataGridView1.Columns.Add(column4);

        }

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Monday, August 17, 2009 8:25 AM
Hi Rebacca,
If you want selectedIndexChanged of datagridviewCombocolumn, thenYou can handle the cellEditingControlShowing event, add selectedIndexChanged event to the combobox, thus you can get the selected value. The following code demostrates how to do this:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    ComboBox cb = e.Control as ComboBox;
    if (cb != null)
    {
        // first remove event handler to keep from attaching multiple:
        cb.SelectedIndexChanged -= new EventHandler(cb_SelectedIndexChanged); 

        // now attach the event handler
        cb.SelectedIndexChanged += new   EventHandler(cb_SelectedIndexChanged);
    }

} 

void cb_SelectedIndexChanged(object sender, EventArgs e)
{ 
      ComboBox cb = sender as ComboBox;
MessageBox.Show(cb.selectedValue.toString());
}


if above not help you, please provider more details or code.
NareshG  Thursday, August 13, 2009 6:08 AM

Hi,
I hope it will help you

private void Form1_Load(object sender, EventArgs e)
        {
            AddColumns();
            dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
        }

        void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {

            if (dataGridView1.CurrentCell.ColumnIndex == 0)
            {
                ComboBox cb = e.Control as ComboBox;
                if (cb != null)
                {
                    cb.SelectedIndexChanged -= new EventHandler(cb_SelectedIndexChanged);
                    cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
                }
            }
            
        }

        void cb_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataGridViewComboBoxColumn cbo = dataGridView1.Columns[1] as DataGridViewComboBoxColumn;
            cbo.Items.AddRange("Plan1", "Plan2", "Plan3", "Plan4", "Plan5");
            
        }
        private void AddColumns()
        {
            DataGridViewComboBoxColumn column1 = new DataGridViewComboBoxColumn();
            column1.HeaderText = "Treatement";
            column1.Width = 90;
            column1.FlatStyle = FlatStyle.Flat;
            column1.Items.AddRange("Treatment1", "Treatment2", "Treatment3", 
"Treatment4", "Treatment5");         
        
            DataGridViewComboBoxColumn column2 = new DataGridViewComboBoxColumn();
            column2.HeaderText = "TreatementPlan";
            column2.Width = 90;
            column2.FlatStyle = FlatStyle.Flat;           
            DataGridViewTextBoxColumn column3 = new DataGridViewTextBoxColumn();
            column3.HeaderText = "Action1";
            column3.Width = 90;
            DataGridViewTextBoxColumn column4 = new DataGridViewTextBoxColumn();
            column4.HeaderText = "Action2";
            column4.Width = 90;
            dataGridView1.Columns.Add(column1);
            dataGridView1.Columns.Add(column2);
            dataGridView1.Columns.Add(column3);
            dataGridView1.Columns.Add(column4);

        }

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Monday, August 17, 2009 8:25 AM
Hi guys! Thanks so much for your replies, I created a quick workaround but will try do it more elegantly later.

I thought I'd ticked to recieve emails when I got replies to my posts, obvisouly somewhere it went wrong because I didn't know I had recieved replies.

I will use your example codes when I move back to tidying up the project :),

Thanks heaps again!


Rebecca
RebeccaAtDragonFly  Tuesday, September 08, 2009 9:01 PM

You can use google to search for other answers

Custom Search

More Threads

• Compare two datasets
• custom painting datagridview
• How to insert the date of the system as DefaultValue in a Dataset
• custom dgv, binding, repeating columns
• How to build a group-filter, based on multiple ListBox ?
• vb will write record to sql one time and not the next
• Trouble updating SQL database from vb.net 2003
• UpdateCommand in GridView
• Question about binding XML with DataGridView and ArrayList.
• Filtering a DataGridViewComboBoxColumn