设置的DataGridView列类型下拉在运行时(C#.NET)类型、DataGridView、NET

2023-09-04 23:32:20 作者:屋檐上

我有一个DataGridView的数据源设置为数据视图 - >数据表和行和放大器;列汽车在运行时产生的。

I have a datagridview its datasource is set to a data view -> data table and rows & columns are auto generated at run time.

    dtable.Columns.Add("1", typeof(int));
    dtable.Columns.Add("2", typeof(string));
    dtable.Columns.Add("3", typeof(string));
    datagridview1.DataSource = dtable;

我想塔3是一个​​下拉式和有3个选择在它。我该怎么做,在运行时,因为我只填充网格在运行时。

I want column 3 to be a drop down type and have 3 options in it. How do I do that at run time as I only populate the grid at run time.

推荐答案

您可以添加列到的DataGridView 编程。

在你的情况是什么,你需要做的是在绑定到数据表,然后添加一个 DataGridViewComboBoxColumn 来有第三纵队的名称作为它的 DataPropertyName 属性的网格。

In your situation what you need to do is hide the column 3 that is generated when you bind to the DataTable and then add a DataGridViewComboBoxColumn to the grid which has column three's name as its DataPropertyName property.

DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
col.DataPropertyName = "3";
dataGridView1.Columns.Add(col);

您再指定一个数据源此列,其中的数据源包含要显示在网格中的三个选项。

You then assign a datasource to this column where that datasource holds the three options you want to appear in the grid.

DataGridViewComboBoxColumn 也有 ValueMember 的DisplayMember 属性允许你控制什么获取网格中的数据源设置( ValueMember ),并显示的内容。

The DataGridViewComboBoxColumn also has ValueMember and DisplayMember properties that allow you control what gets set in the grid datasource (ValueMember) and what is displayed.