c# - 将 DataGridView 列类型设置为在运行时下拉 (C#.net)

标签 c# .net datagridview combobox drop-down-menu

我有一个数据 GridView ,它的数据源设置为数据 View -> 数据表和行和列在运行时自动生成。

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

我希望第 3 列是下拉类型,并且其中有 3 个选项。我如何在运行时执行此操作,因为我只在运行时填充网格。

最佳答案

您可以通过编程方式将列添加到 DataGridView

在您的情况下,您需要做的是隐藏绑定(bind)到 DataTable 时生成的第 3 列,然后将 DataGridViewComboBoxColumn 添加到具有列的网格三的名称作为其 DataPropertyName 属性。

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

然后您将一个数据源分配给该列,该数据源包含您希望在网格中显示的三个选项。

DataGridViewComboBoxColumn 还具有 ValueMemberDisplayMember 属性,允许您控制在网格数据源中设置的内容 (ValueMember) 以及显示的内容。

关于c# - 将 DataGridView 列类型设置为在运行时下拉 (C#.net),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7840341/

相关文章:

c# - 在显示特定产品时,电子商务网站更可取的是什么?

c# - DataGridViewComboBoxColumn 不会在第一次单击时打开下拉列表

c# - 无法更改 datagridview winforms 中的图像

c# - 当列表绑定(bind)到 datagridview 时修复列宽

c# - 如何通过单击另一种形式的按钮来运行 Game1

c# - Protobuf-net 使用接口(interface)和抽象基类创建类型模型

c# - 设置窗口位置

c# - 如何将 Windows 窗体元素发送到 wpf 应用程序的后面?

c# - ASP.NET MVC5 Google APIs GoogleWebAuthorizationBroker.AuthorizeAsync 在本地工作但未部署到 IIS

c# - C# 中的对象、参数和 ref 关键字