Click here to Skip to main content
14,734,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends..

I need to change the datagridview cell type to combobox while editing. and return back to text column after the editing.
My datagridview is a bounded with list
Here is the sample code
public class Car
   {
       private string _company;
       private string _model;
       private int _year;

       public string Company
       {
           get { return _company; }
           set { _company = value; }
       }
       public string Model
       {
           get { return _model; }
           set { _model = value; }
       }
       public int Year
       {
           get { return _year; }
           set { _year = value; }
       }
       public Car(string make, string model, int year)
         {
           _company = make;
           _model = model;
           _year = year;
         }
   }


In the form load event
private void Form1_Load(object sender, EventArgs e)
        {
           
            List<Car> li=new List<Car>();
            li.Add(new Car("Maruthi","Swift",2000));
            li.Add(new Car("Maruthi", "Wagnor", 2005));
            li.Add(new Car("Maruthi", "Ritz", 2003));

            li.Add(new Car("Toyota", "Etios", 2012));
            li.Add(new Car("Toyota", "Innova", 2008));
            li.Add(new Car("Toyota", "Fortuner", 2010));

            dgvcar.DataSource = li;

        }
    }

private void dgvcar_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            int colIndex = dgvcar.CurrentCell.ColumnIndex;
            int rowIndex = dgvcar.CurrentCell.RowIndex;

            if (colIndex == 1)
            {
                e.CellStyle.BackColor = Color.Aqua;
                //e.CellStyle.
                DataGridViewComboBoxCell cb = new DataGridViewComboBoxCell();
                cb.Items.Add("Etios");
                cb.Items.Add("Innova");
                cb.Items.Add("Fortuner");
                dgvcar[colIndex, rowIndex] = cb;

            }
        }
Posted
Updated 29-Dec-14 1:27am
v2

1 solution

Hi,

I can suggest one way to achieve this.
Lets say you have one gridview/datagrid. While binding the data to the datagrid or gridview you can bind that using the load event and the field will be having the textbox control. While editing the data using the itemData bound method you can able to replace the textbox control into a dropdown control. Once your editing id completed then while updating the data you change the control to textbox again.

Try to implement this logic.

Thanks
Sisir Patro
   
Comments
jinesh sam 29-Dec-14 6:25am
   
itemData bound method is related to web applications and i am dealing with desktop applications.

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