none
DatagridView combobox selection change

    Question

  • please help me...

    I  need to pass values to cell next to comboboxcell in datagridview on its selection changed.....

     

    Saturday, December 29, 2007 10:52 AM

Answers

  •  

    Hi ssangeetha,

    You can handle the EditingControlShowing event to get the ComboBox hosted in the ComboBox cell and register the SelectedIndexChanged event of a ComboBox in this handler. And then you can pass the value in this SelectedIndexChanged event handler. Check my sample below:

    Code Block

    namespace NEWDGV

    {

        public partial class Form14 : Form

        {

            public Form14()

            {

                InitializeComponent();

            }

     

            DataTable dt = new DataTable();

     

            private void Form14_Load(object sender, EventArgs e)

            {

                DataTable typedt = new DataTable();

                typedt.Columns.Add("typeid", typeof(int));

                typedt.Columns.Add("typename");

                typedt.Columns.Add("typedescription");

                typedt.Rows.Add(0, "Typed 0","description0");

                typedt.Rows.Add(1, "Typed 1","description1");

                typedt.Rows.Add(2, "Typed 2","description2");

                typedt.Rows.Add(3, "Typed 3","description3");

                typedt.Rows.Add(4, "Typed 4","description4");

                typedt.AcceptChanges();

     

                dt.Columns.Add("id");

                dt.Columns.Add("name");

                dt.Columns.Add("type", typeof(int));

                dt.Columns.Add("typedescription");

                for (int i = 0; i < 50; i++)

                {

                    dt.Rows.Add(i.ToString("000"), "name" + i, i % 4);

                }

                dt.AcceptChanges();

                this.dataGridView1.DataSource = dt;

                this.dataGridView1.Columns.RemoveAt(2);

                DataGridViewComboBoxColumn cbc = new DataGridViewComboBoxColumn();

                cbc.DataPropertyName = "type";

                cbc.DisplayMember = "typename";

                cbc.ValueMember = "typeid";

                cbc.DataSource = typedt;

                cbc.HeaderText = "Type";

                this.dataGridView1.Columns.Insert(2, cbc);

                this.dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);

                this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);

            }

     

            void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)

            {

                if (cbm != null)

                {

                    cbm.SelectedIndexChanged -= new EventHandler(cbm_SelectedIndexChanged);

                }

            }

     

            ComboBox cbm;

            DataGridViewCell currentCell;

            void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

            {

                if (e.Control is ComboBox)

                {

                    cbm = (ComboBox)e.Control;

                    if (cbm != null)

                    {

                        cbm.SelectedIndexChanged += new EventHandler(cbm_SelectedIndexChanged);

                    }

                    currentCell = this.dataGridView1.CurrentCell;

                }

            }

     

            void cbm_SelectedIndexChanged(object sender, EventArgs e)

            {

                this.BeginInvoke(new MethodInvoker(EndEdit));

            }

     

            void EndEdit()

            {

                if (cbm != null)

                {

                    DataRowView drv = cbm.SelectedItem as DataRowView;

                    if (drv != null)

                    {

                        this.dataGridView1[currentCell.ColumnIndex + 1, currentCell.RowIndex].Value = drv[2].ToString();

                        this.dataGridView1.EndEdit();

                    }

                }

            }

        }

    }

     

     

    Hope this helps.
    Best regards and Happy New Year.
    Rong-Chun Zhang

    Tuesday, January 01, 2008 6:30 AM

All replies

  •  

    Hi ssangeetha,

    You can handle the EditingControlShowing event to get the ComboBox hosted in the ComboBox cell and register the SelectedIndexChanged event of a ComboBox in this handler. And then you can pass the value in this SelectedIndexChanged event handler. Check my sample below:

    Code Block

    namespace NEWDGV

    {

        public partial class Form14 : Form

        {

            public Form14()

            {

                InitializeComponent();

            }

     

            DataTable dt = new DataTable();

     

            private void Form14_Load(object sender, EventArgs e)

            {

                DataTable typedt = new DataTable();

                typedt.Columns.Add("typeid", typeof(int));

                typedt.Columns.Add("typename");

                typedt.Columns.Add("typedescription");

                typedt.Rows.Add(0, "Typed 0","description0");

                typedt.Rows.Add(1, "Typed 1","description1");

                typedt.Rows.Add(2, "Typed 2","description2");

                typedt.Rows.Add(3, "Typed 3","description3");

                typedt.Rows.Add(4, "Typed 4","description4");

                typedt.AcceptChanges();

     

                dt.Columns.Add("id");

                dt.Columns.Add("name");

                dt.Columns.Add("type", typeof(int));

                dt.Columns.Add("typedescription");

                for (int i = 0; i < 50; i++)

                {

                    dt.Rows.Add(i.ToString("000"), "name" + i, i % 4);

                }

                dt.AcceptChanges();

                this.dataGridView1.DataSource = dt;

                this.dataGridView1.Columns.RemoveAt(2);

                DataGridViewComboBoxColumn cbc = new DataGridViewComboBoxColumn();

                cbc.DataPropertyName = "type";

                cbc.DisplayMember = "typename";

                cbc.ValueMember = "typeid";

                cbc.DataSource = typedt;

                cbc.HeaderText = "Type";

                this.dataGridView1.Columns.Insert(2, cbc);

                this.dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);

                this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);

            }

     

            void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)

            {

                if (cbm != null)

                {

                    cbm.SelectedIndexChanged -= new EventHandler(cbm_SelectedIndexChanged);

                }

            }

     

            ComboBox cbm;

            DataGridViewCell currentCell;

            void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

            {

                if (e.Control is ComboBox)

                {

                    cbm = (ComboBox)e.Control;

                    if (cbm != null)

                    {

                        cbm.SelectedIndexChanged += new EventHandler(cbm_SelectedIndexChanged);

                    }

                    currentCell = this.dataGridView1.CurrentCell;

                }

            }

     

            void cbm_SelectedIndexChanged(object sender, EventArgs e)

            {

                this.BeginInvoke(new MethodInvoker(EndEdit));

            }

     

            void EndEdit()

            {

                if (cbm != null)

                {

                    DataRowView drv = cbm.SelectedItem as DataRowView;

                    if (drv != null)

                    {

                        this.dataGridView1[currentCell.ColumnIndex + 1, currentCell.RowIndex].Value = drv[2].ToString();

                        this.dataGridView1.EndEdit();

                    }

                }

            }

        }

    }

     

     

    Hope this helps.
    Best regards and Happy New Year.
    Rong-Chun Zhang

    Tuesday, January 01, 2008 6:30 AM
  • hai zhang..

    HAPPY NEW YEAR

    your post was very help full.....

     

    U HAVE SOLVED MY PROBLEM

     

    THANKS

    SANGEETHA

     

    Friday, January 11, 2008 10:23 AM