繁体   English   中英

DataGridView将TextBoxColumn转换为ComboBoxColumn

[英]DataGridView Convert TextBoxColumn to ComboBoxColumn

我有一个简单的数据库(SQL),汽车表有六个字段:ID,所有者,年份,颜色,品牌和型号。 颜色表就是:ID,ColorName。

我有一个带有DataGridView的窗体。 当我将Cars表绑定到DataGridView时,到此为止一切正常。

现在,我正在尝试使DataGridView中的“颜色”列成为用于编辑的comboBox,并仅列出我的“颜色表”中的颜色。 我可以制作一个comboBox并将其添加到DataGridView,但我想只是“转换”已包含数据的列。

在此先感谢您提供的任何帮助。

using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

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

            var myDataBase = new CarsEntities();
            List<MyCar> data = myDataBase.MyCars.OrderBy(o => o.Owner).ToList();

            //dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = data;

            const int index = 2; // Column Index for Color

            List<string> myColor = myDataBase.Colors.Select(s => s.Color1).ToList();

            var comboBoxColumn = new DataGridViewComboBoxColumn
            {
                DataSource = myColor,
                DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing,
                AutoComplete = true,
            };
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].EditType.ToString());
        }
    }
}

基于此主题->“将Databound DataGridView上的单元格类型更改为Custom DataGridViewCell Type ”主题,建议您进行以下操作:

public partial class Form1 : Form
{
    List<string> availableCarColors = new List<string>();
    List<MyCar> data = new List<MyCar>();

    public Form1()
    {
        InitializeComponent();

        availableCarColors.Add("Red");
        availableCarColors.Add("Blue");

        data.Add(new MyCar("Lexus", "Blue"));
        data.Add(new MyCar("BMW", "Red"));

        DataGridViewTextBoxColumn colModel = new DataGridViewTextBoxColumn();
        colModel.Name = "Model";                //Header Text
        colModel.DataPropertyName = "Model";    //Property Name of my car class

        DataGridViewComboBoxColumn colColor = new DataGridViewComboBoxColumn();
        colColor.Name = "Color";
        colColor.DataPropertyName = "Color";
        colColor.DataSource = availableCarColors;

        dataGridView1.AutoGenerateColumns = false;
        dataGridView1.Columns.AddRange(new DataGridViewColumn[] {colModel, colColor});
        dataGridView1.DataSource = data;
    }

    class MyCar
    {
        public string Model { get; set; }
        public string Color { get; set; }

        public MyCar(string model, string color)
        {
            Model = model;
            Color = color;
        }
    }
}

您必须使用以下设置使用“颜色”表数据填充DataGridViewComboBoxColumn:

我只想发布如何使用@Tezzo的解决方案来获得所需的结果:

using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace EditingCells
{
    public partial class Form1 : Form
    {
        private readonly CarsEntities _myDataBase = new CarsEntities();

        public Form1()
        {
            InitializeComponent();

            List<MyCar> myData = _myDataBase.MyCars.ToList();

            // Yes, you have do make Columns manually.
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = myData;

            List<Color> myColors = _myDataBase.Colors.ToList();

            var carOwner = new DataGridViewTextBoxColumn
                {
                    DataPropertyName = "Owner",
                    HeaderText = @"Owner",
                    Name = "Owner",
                };
            dataGridView1.Columns.Add(carOwner);

            var carColor = new DataGridViewComboBoxColumn
                {
                    // Field Name where existing data is stored
                    DataPropertyName = "CarColor",
                    HeaderText = @"Color",
                    Name = "Color",
                    DataSource = myColors,
                    DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing,
                    AutoComplete = true,

                    // Field value from DataSource to store in Car.Color field
                    ValueMember = "ColorName",

                    // Field value from DataSource to show in DataGridView Column
                    DisplayMember = "ColorName",
                };
            dataGridView1.Columns.Add(carColor);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            _myDataBase.SaveChanges();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM