Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView ComboBoxColumn setting a default value (bound ComboBoxColumn)
 

DataGridView ComboBoxColumn setting a default value (bound ComboBoxColumn)

I have a small example where I have some swipe cards. I am tracking where the cards are. I have a SQL database and three tables - CustomerFile, CardFile and StatusFile.
I have a DataGridView that is bound to the CardFile
It has three columns - CardId, CardStatus (ComboBox) and Customer (ComboBox)
CardStatus (ComboBox) has PropertyName set to the CardFile - and the display and value come from StatusFile. The statusFile has three choices - In Inventory, Out to Customer, Damaged Card
The Customer (ComboBox) has PropertyName set to CardFile - and the display and value come from CustomerFile.

In the Customer file, I only have valid customer - I don't have a customer with CustomerId of -1 and Name of "No Customer".

However, in my situation if the user selects "In Inventory" or "Card Damaged" I want to set the CustomerComboBox to "No Customer".

I am using BindingSource for each of the datatables. So BindingSource3 - DataSource = datasetCustomer and DataMember is CustomerFile

The CustomerFile's CustomerId is the PrimaryKey and is also set for autoincrementing.

At runtime - how can I add to the Customer ComboBox a "No Customer" with a behind value of -1?

Thanks, John
JohnFeeney  Friday, October 02, 2009 2:50 PM

Hello John,

Thanks for your post.

First, let me check my understanging with you. You have a DataGridView with two DataGridViewComboBox columns, one for status and the other for customer. You want to fill the customer column with No customer when when user change status to "In Inventory" or "Card Damaged". Please correct me if there is any misunderstanding.

In my opinion, a simple way is that we can add a "new" customer to the customers table with value of -1, when users select "In Inventory" or "Card Damaged", we can set the customer combox cell's value to -1, then it will displays "No Customer". You can check the following thread for the details to change another cell's value based on the selection of a combobox cell.

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/aef28e05-f368-4148-99ef-480d58638c8a/

Another idea is to change the ComboBox cell to TextBox cell when users select "In Inventory" or "Card Damaged" and set the cell's value to DBNull(this column needs to allow null value in the database), we can then set the cell readonly to true to not allow use change the value and handle the cellformating event the make the cell diaplays "No Customer". The following code shows you a simple sample for replace a ComboxCell to TextBoxCell depend on the selection of the checkbox.

<code>
        DataTable dt = new DataTable();

        private void CboAndTxt_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("aa", typeof(bool));
            dt.Columns.Add("bb");

            for (int i = 1; i < 20; i++)
            {
                if (i % 2 == 0)
                    dt.Rows.Add(true);
                else
                    dt.Rows.Add(false,"item1");
            }

            dt.AcceptChanges();

            this.dataGridView1.DataSource = dt;

            this.dataGridView1.Columns.RemoveAt(1);
            DataGridViewComboBoxColumn cbc = new DataGridViewComboBoxColumn();
            cbc.HeaderText = "bb";
            cbc.Items.AddRange(new object[] { "item1", "item2", "item3" });
            cbc.DataPropertyName = "bb";
            this.dataGridView1.Columns.Add(cbc);

            foreach (DataGridViewRow dr in this.dataGridView1.Rows)
            {
                if (dr.Cells[0].Value != null)
                {
                    if ((bool)dr.Cells[0].Value)
                    {
                        DataGridViewTextBoxCell txtcell = new DataGridViewTextBoxCell();
                        dr.Cells[1] = txtcell;
                        dr.Cells[1].ReadOnly = true;
                    }
                }
            }

            this.dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
            this.dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
        }

        void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 1 && e.RowIndex != this.dataGridView1.NewRowIndex)
            {
                if (e.Value == DBNull.Value)
                    e.Value = "No Customer";
                e.FormattingApplied = true;
            }
        }

        void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                if (this.dataGridView1[0, e.RowIndex].Value != null)
                {
                    if ((bool)this.dataGridView1[0, e.RowIndex].Value)
                    {
                        DataGridViewTextBoxCell txtcell = new DataGridViewTextBoxCell();
                        this.dataGridView1.Rows[e.RowIndex].Cells[1] = txtcell;
                        this.dataGridView1.Rows[e.RowIndex].Cells[1].ReadOnly = true;
                        DataRowView drv = this.dataGridView1.Rows[e.RowIndex].DataBoundItem as DataRowView;
                        if (drv != null)
                            drv[1] = DBNull.Value;
                    }
                    else
                    {
                        DataGridViewComboBoxCell cbocell = new DataGridViewComboBoxCell();
                        DataGridViewComboBoxColumn cm = this.dataGridView1.Columns[1] as DataGridViewComboBoxColumn;
                        cbocell.Items.AddRange(new object[] { "item1", "item2", "item3" });
                        this.dataGridView1.Rows[e.RowIndex].Cells[1] = cbocell;
                        this.dataGridView1.Rows[e.RowIndex].Cells[1].ReadOnly = false;
                        this.dataGridView1.Rows[e.RowIndex].Cells[1].Value = "item1";
                    }
                }
            }
        }
</code>

Thanks,
Rong-Chun Zhang
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg[at]microsoft.com


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Marked As Answer byJohnFeeney Thursday, October 08, 2009 12:23 AM
  •  
Rong-Chun Zhang  Monday, October 05, 2009 11:04 AM

Hello John,

Thanks for your post.

First, let me check my understanging with you. You have a DataGridView with two DataGridViewComboBox columns, one for status and the other for customer. You want to fill the customer column with No customer when when user change status to "In Inventory" or "Card Damaged". Please correct me if there is any misunderstanding.

In my opinion, a simple way is that we can add a "new" customer to the customers table with value of -1, when users select "In Inventory" or "Card Damaged", we can set the customer combox cell's value to -1, then it will displays "No Customer". You can check the following thread for the details to change another cell's value based on the selection of a combobox cell.

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/aef28e05-f368-4148-99ef-480d58638c8a/

Another idea is to change the ComboBox cell to TextBox cell when users select "In Inventory" or "Card Damaged" and set the cell's value to DBNull(this column needs to allow null value in the database), we can then set the cell readonly to true to not allow use change the value and handle the cellformating event the make the cell diaplays "No Customer". The following code shows you a simple sample for replace a ComboxCell to TextBoxCell depend on the selection of the checkbox.

<code>
        DataTable dt = new DataTable();

        private void CboAndTxt_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("aa", typeof(bool));
            dt.Columns.Add("bb");

            for (int i = 1; i < 20; i++)
            {
                if (i % 2 == 0)
                    dt.Rows.Add(true);
                else
                    dt.Rows.Add(false,"item1");
            }

            dt.AcceptChanges();

            this.dataGridView1.DataSource = dt;

            this.dataGridView1.Columns.RemoveAt(1);
            DataGridViewComboBoxColumn cbc = new DataGridViewComboBoxColumn();
            cbc.HeaderText = "bb";
            cbc.Items.AddRange(new object[] { "item1", "item2", "item3" });
            cbc.DataPropertyName = "bb";
            this.dataGridView1.Columns.Add(cbc);

            foreach (DataGridViewRow dr in this.dataGridView1.Rows)
            {
                if (dr.Cells[0].Value != null)
                {
                    if ((bool)dr.Cells[0].Value)
                    {
                        DataGridViewTextBoxCell txtcell = new DataGridViewTextBoxCell();
                        dr.Cells[1] = txtcell;
                        dr.Cells[1].ReadOnly = true;
                    }
                }
            }

            this.dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
            this.dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
        }

        void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 1 && e.RowIndex != this.dataGridView1.NewRowIndex)
            {
                if (e.Value == DBNull.Value)
                    e.Value = "No Customer";
                e.FormattingApplied = true;
            }
        }

        void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                if (this.dataGridView1[0, e.RowIndex].Value != null)
                {
                    if ((bool)this.dataGridView1[0, e.RowIndex].Value)
                    {
                        DataGridViewTextBoxCell txtcell = new DataGridViewTextBoxCell();
                        this.dataGridView1.Rows[e.RowIndex].Cells[1] = txtcell;
                        this.dataGridView1.Rows[e.RowIndex].Cells[1].ReadOnly = true;
                        DataRowView drv = this.dataGridView1.Rows[e.RowIndex].DataBoundItem as DataRowView;
                        if (drv != null)
                            drv[1] = DBNull.Value;
                    }
                    else
                    {
                        DataGridViewComboBoxCell cbocell = new DataGridViewComboBoxCell();
                        DataGridViewComboBoxColumn cm = this.dataGridView1.Columns[1] as DataGridViewComboBoxColumn;
                        cbocell.Items.AddRange(new object[] { "item1", "item2", "item3" });
                        this.dataGridView1.Rows[e.RowIndex].Cells[1] = cbocell;
                        this.dataGridView1.Rows[e.RowIndex].Cells[1].ReadOnly = false;
                        this.dataGridView1.Rows[e.RowIndex].Cells[1].Value = "item1";
                    }
                }
            }
        }
</code>

Thanks,
Rong-Chun Zhang
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg[at]microsoft.com


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Marked As Answer byJohnFeeney Thursday, October 08, 2009 12:23 AM
  •  
Rong-Chun Zhang  Monday, October 05, 2009 11:04 AM


Hello,

I am writing to check the status of the issue on your side. Would you mind letting me know the result of the suggestions? If you have any additional question, welcome to post here.

Have a great day!

Thanks,
Rong-Chun Zhang
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg[at]microsoft.com


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Rong-Chun Zhang  Wednesday, October 07, 2009 9:32 AM
thanks very much for your reply and your idea....I opened a new project and pasted your code into it. I then did a quick modification that added another comboboxcolumn to the project and was able to duplicate your idea by having Inventory and discard be textbox and Customer be a drop down list.

I then added a regular combobox control to my page and put a textbox beside it. At the end of the Form1.Load event I added three items to the combobox1 - testInventory, testCustomer, testDiscard. When I run the app, the code watches the ComboBox1 SelectedIndexChanged. As you change values in combobox1 the values IMMEDIATELY change in TextBox1.

Now if I go back to the combobox that is inside the datagrid, there seems to be a disconnect. First when you first click on the combobox nothing happens. Then if you click on it again the dropdown list appears. You can pick from it and the new value will show. However, the program does not react to the new value. You can click again and it will show the new value...however nothing is changing in the column that we have titled "bb". It is not until you move out of the cell by clicking on a different row or a different cell within the row that the program reacts to the change in the datagrid cell.

I think the user is going to find this confusing. I assume this is the only way that the datagridview combobox will work -- by this I mean that I have to click twice (not double click -- two single clicks) to get the list to drop down and that I have to leave the cell to get other columns to react to the changes made to the cell.

Thanks, John -- PS I will include my modified code below

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CboAndTxt
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        DataTable dt = new DataTable();

        private void Form1_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("aa", typeof(bool));
            dt.Columns.Add("bb");
            

            for (int i = 1; i < 20; i++)
            {
                if (i % 2 == 0)
                    dt.Rows.Add(true);
                else
                    dt.Rows.Add(false, "item1");
            }

            dt.AcceptChanges();

            this.dataGridView1.DataSource = dt;

            this.dataGridView1.Columns.RemoveAt(1);
            DataGridViewComboBoxColumn cbc = new DataGridViewComboBoxColumn();
            cbc.HeaderText = "bb";
            cbc.Items.AddRange(new object[] { "item1", "item2", "item3" });
            cbc.DataPropertyName = "bb";
            this.dataGridView1.Columns.Add(cbc);
            // added by John
            DataGridViewComboBoxColumn cbjohn = new DataGridViewComboBoxColumn();
            cbjohn.HeaderText = "another";
            cbjohn.Items.AddRange(new object[] {"Inventory", "Customer", "Discard"});
            //cbjohn.DataPropertyName = ?? not set ??
            this.dataGridView1.Columns.Add(cbjohn);

            foreach (DataGridViewRow dr in this.dataGridView1.Rows)
            {
                if (dr.Cells[0].Value != null)
                {
                    if ((bool)dr.Cells[0].Value)
                    {
                        DataGridViewTextBoxCell txtcell = new DataGridViewTextBoxCell();
                        dr.Cells[1] = txtcell;
                        dr.Cells[1].ReadOnly = true;
                    }
                }
            }

            this.dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
            this.dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
            //
            // added by John
            comboBox1.Items.Add("testInventory");
            comboBox1.Items.Add("testCustomer");
            comboBox1.Items.Add("testDiscard");
        }

        void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 1 && e.RowIndex != this.dataGridView1.NewRowIndex)
            {
                if (e.Value == DBNull.Value)
                    e.Value = "No Customer";
                e.FormattingApplied = true;
            }
            
        }

        void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                MessageBox.Show("Col 0");
                if (this.dataGridView1[0, e.RowIndex].Value != null)
                {
                    if ((bool)this.dataGridView1[0, e.RowIndex].Value)
                    {
                        DataGridViewTextBoxCell txtcell = new DataGridViewTextBoxCell();
                        this.dataGridView1.Rows[e.RowIndex].Cells[1] = txtcell;
                        this.dataGridView1.Rows[e.RowIndex].Cells[1].ReadOnly = true;
                        DataRowView drv = this.dataGridView1.Rows[e.RowIndex].DataBoundItem as DataRowView;
                        if (drv != null)
                            drv[1] = DBNull.Value;
                    }
                    else
                    {
                        DataGridViewComboBoxCell cbocell = new DataGridViewComboBoxCell();
                        DataGridViewComboBoxColumn cm = this.dataGridView1.Columns[1] as DataGridViewComboBoxColumn;
                        cbocell.Items.AddRange(new object[] { "item1", "item2", "item3" });
                        this.dataGridView1.Rows[e.RowIndex].Cells[1] = cbocell;
                        this.dataGridView1.Rows[e.RowIndex].Cells[1].ReadOnly = false;
                        this.dataGridView1.Rows[e.RowIndex].Cells[1].Value = "item1";
                    }
                }
            }
            if (e.ColumnIndex == 2)
            {
                MessageBox.Show("Col 2");
                if (this.dataGridView1[2, e.RowIndex].Value.ToString() != "Customer")
                {
                    DataGridViewTextBoxCell txtcell = new DataGridViewTextBoxCell();
                    this.dataGridView1.Rows[e.RowIndex].Cells[1] = txtcell;
                    this.dataGridView1.Rows[e.RowIndex].Cells[1].ReadOnly = true;
                    DataRowView drv = this.dataGridView1.Rows[e.RowIndex].DataBoundItem as DataRowView;
                    if (drv != null)
                        drv[1] = DBNull.Value;
                }
                else
                {
                    DataGridViewComboBoxCell cbocell = new DataGridViewComboBoxCell();
                    DataGridViewComboBoxColumn cm = this.dataGridView1.Columns[1] as DataGridViewComboBoxColumn;
                    cbocell.Items.AddRange(new object[] { "item1", "item2", "item3" });
                    this.dataGridView1.Rows[e.RowIndex].Cells[1] = cbocell;
                    this.dataGridView1.Rows[e.RowIndex].Cells[1].ReadOnly = false;
                    this.dataGridView1.Rows[e.RowIndex].Cells[1].Value = "item1";
                }
            }
        }

        // added by John<br />        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show("in SelectedIndexChanged");
            if (comboBox1.SelectedText.ToString() != "testCustomer")
            {
                textBox1.Text = "None";
            }
            else
            {
                textBox1.Text = "Customer ComboBox";
            }
        }
    }
}
JohnFeeney  Wednesday, October 07, 2009 8:40 PM
I have just discovered one solution to my problem. In the DataGridView there is a property called EditMode. By Default this is set to EditOnKeyStrokeOrF2. If you set this to EditOnEnter then you get rid of the "two single clicks" required to get the combobox to drop down right away.

JohnFeeney  Wednesday, October 07, 2009 8:52 PM
After doing some reading on DataGridView ComboBoxColumn SelectedIndexChanged I found an example at MSDN.

Now when I click the ComboBox in the GridView (the one with Headertext - Another). I receive the following Messagebox.Show messages:
"In EditingControlShowing"
"combo !=null"
"I did reach myComboSelectedIndexChanged"

So, I am getting to the right subroutine that should be able to change TextBox1.Text and replace with the name of the Comboboxcolumn value.

However, all I get displayed in the Textbox1.Text is "This plus"....so I am not getting the combobox value.

What should this line be to get the combobox value?

textBox1.Text =

"This plus " + ((ComboBox)sender).SelectedText.ToString();

Here is my whole code sample now

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CboAndTxt
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        DataTable dt = new DataTable();

        private void Form1_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("aa", typeof(bool));
            dt.Columns.Add("bb");


            for (int i = 1; i < 20; i++)
            {
                if (i % 2 == 0)
                    dt.Rows.Add(true);
                else
                    dt.Rows.Add(false, "item1");
            }

            dt.AcceptChanges();

            this.dataGridView1.DataSource = dt;

            this.dataGridView1.Columns.RemoveAt(1);
            DataGridViewComboBoxColumn cbc = new DataGridViewComboBoxColumn();
            cbc.HeaderText = "bb";
            cbc.Items.AddRange(new object[] { "item1", "item2", "item3" });
            cbc.DataPropertyName = "bb";
            this.dataGridView1.Columns.Add(cbc);
            // added by John
            DataGridViewComboBoxColumn cbjohn = new DataGridViewComboBoxColumn();
            cbjohn.HeaderText = "another";
            cbjohn.Items.AddRange(new object[] { "Inventory", "Customer", "Discard" });
            //cbjohn.DataPropertyName = ?? not set ??
            this.dataGridView1.Columns.Add(cbjohn);

            foreach (DataGridViewRow dr in this.dataGridView1.Rows)
            {
                if (dr.Cells[0].Value != null)
                {
                    if ((bool)dr.Cells[0].Value)
                    {
                        DataGridViewTextBoxCell txtcell = new DataGridViewTextBoxCell();
                        dr.Cells[1] = txtcell;
                        dr.Cells[1].ReadOnly = true;
                    }
                }
            }

            this.dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
            this.dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
            //
            // added by John
            comboBox1.Items.Add("testInventory");
            comboBox1.Items.Add("testCustomer");
            comboBox1.Items.Add("testDiscard");
        }

        void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 1 && e.RowIndex != this.dataGridView1.NewRowIndex)
            {
                if (e.Value == DBNull.Value)
                    e.Value = "No Customer";
                e.FormattingApplied = true;
            }

        }

        void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                MessageBox.Show("Col 0");
                if (this.dataGridView1[0, e.RowIndex].Value != null)
                {
                    if ((bool)this.dataGridView1[0, e.RowIndex].Value)
                    {
                        DataGridViewTextBoxCell txtcell = new DataGridViewTextBoxCell();
                        this.dataGridView1.Rows[e.RowIndex].Cells[1] = txtcell;
                        this.dataGridView1.Rows[e.RowIndex].Cells[1].ReadOnly = true;
                        DataRowView drv = this.dataGridView1.Rows[e.RowIndex].DataBoundItem as DataRowView;
                        if (drv != null)
                            drv[1] = DBNull.Value;
                    }
                    else
                    {
                        DataGridViewComboBoxCell cbocell = new DataGridViewComboBoxCell();
                        DataGridViewComboBoxColumn cm = this.dataGridView1.Columns[1] as DataGridViewComboBoxColumn;
                        cbocell.Items.AddRange(new object[] { "item1", "item2", "item3" });
                        this.dataGridView1.Rows[e.RowIndex].Cells[1] = cbocell;
                        this.dataGridView1.Rows[e.RowIndex].Cells[1].ReadOnly = false;
                        this.dataGridView1.Rows[e.RowIndex].Cells[1].Value = "item1";
                    }
                }
            }
            if (e.ColumnIndex == 2)
            {
                MessageBox.Show("Col 2");
                if (this.dataGridView1[2, e.RowIndex].Value.ToString() != "Customer")
                {
                    DataGridViewTextBoxCell txtcell = new DataGridViewTextBoxCell();
                    this.dataGridView1.Rows[e.RowIndex].Cells[1] = txtcell;
                    this.dataGridView1.Rows[e.RowIndex].Cells[1].ReadOnly = true;
                    DataRowView drv = this.dataGridView1.Rows[e.RowIndex].DataBoundItem as DataRowView;
                    if (drv != null)
                        drv[1] = DBNull.Value;
                }
                else
                {
                    DataGridViewComboBoxCell cbocell = new DataGridViewComboBoxCell();
                    DataGridViewComboBoxColumn cm = this.dataGridView1.Columns[1] as DataGridViewComboBoxColumn;
                    cbocell.Items.AddRange(new object[] { "item1", "item2", "item3" });
                    this.dataGridView1.Rows[e.RowIndex].Cells[1] = cbocell;
                    this.dataGridView1.Rows[e.RowIndex].Cells[1].ReadOnly = false;
                    this.dataGridView1.Rows[e.RowIndex].Cells[1].Value = "item1";
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show("in SelectedIndexChanged");
            if (comboBox1.SelectedText.ToString() != "testCustomer")
            {
                textBox1.Text = "None";
            }
            else
            {
                textBox1.Text = "Customer ComboBox";
            }
        }

        //Added by John
        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            MessageBox.Show("In EditingControlShowing");
            ComboBox combo = e.Control as ComboBox;
            if (combo != null)
            {
                MessageBox.Show("combo != null");
                // Remove an existing event-handler, if present, to avoid 
                // adding multiple handlers when the editing control is reused.
                combo.SelectedIndexChanged -= new EventHandler(myComboBox_SelectedIndexChanged);

                // Add the event handler. 
                combo.SelectedIndexChanged += new EventHandler(myComboBox_SelectedIndexChanged);
            }
        }

        private void myComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

            MessageBox.Show("I did reach myComboBox_SelectedIndexChanged");
            //((ComboBox)sender).BackColor = (Color)((ComboBox)sender).SelectedItem;
            textBox1.Text = "This plus " + ((ComboBox)sender).SelectedText.ToString();
        }
    }
}

JohnFeeney  Wednesday, October 07, 2009 10:06 PM
Correct line is

textBox1.Text =

"This plus " + ((ComboBox)sender).Text;

or it could have done

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            ComboBox editingComboBox = (ComboBox)e.Control;
            if(editingComboBox != null)
                editingComboBox.SelectedIndexChanged += new System.EventHandler(this.editingComboBox_SelectedIndexChanged);
        }
private void editingComboBox_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            ComboBox comboBox1 = (ComboBox)sender;
            // Display index
            MessageBox.Show(comboBox1.SelectedIndex.ToString());
            // Display value
            MessageBox.Show(comboBox1.Text);
        }

JohnFeeney  Thursday, October 08, 2009 12:22 AM

You can use google to search for other answers

Custom Search

More Threads

• Composite Control
• Change XSD file
• DataGridViewComboBoxCell conditional dropdown displaying ValueMember not Displaymember
• using dataadapter/datasets with joined tables to fill/edit an datagridview
• How set up foreign key constraint AND datarelation for master/details view?
• Which datarow is showing?
• Decimal Place
• Deleting rows in Winforms Datagrid
• how add a dataset in visual basic 2005 with Microsoft SQL Server (SqlClient)
• I'm going bonkers with DataGridViews