如何在编辑时在运行时更改datagridview单元格类型 [英] how to change the datagridview cell type at runtime while editing

查看:143
本文介绍了如何在编辑时在运行时更改datagridview单元格类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友..



我需要在编辑时将datagridview单元格类型更改为组合框。并在编辑后返回文本列。

我的datagridview与列表有界

这是示例代码

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;

            }
        }

推荐答案





我可以建议一种方法来实现这个目标。

假设您有一个gridview / datagrid。在将数据绑定到datagrid或gridview时,您可以使用加载事件绑定该数据,并且该字段将具有文本框控件。使用 itemData绑定方法编辑数据时,您可以将文本框控件替换为下拉控件。一旦您的编辑ID完成,然后更新数据,您再次将控件更改为文本框。



尝试实现此逻辑。



谢谢

Sisir Patro
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


这篇关于如何在编辑时在运行时更改datagridview单元格类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆