带有数据源和组合框的datagridview

问题描述 投票:0回答:3

我有一个 datagridview,其中附加了一个由自定义数据表组成的数据源:

数据表: 0 = 字典 1 = 字符串 2 = 字符串

datagridview 是可编辑的,但是对于第 0 列,我需要显示一个组合框而不是文本字段。我该如何实现这一目标?

internal Dictionary<int, string> products_list = new Dictionary<int, string>();
products_list.Add(0, "Test Product 1");
products_list.Add(1, "Test Product 2");


lines.Columns.Add(new DataColumn("Product", products_list.GetType()));
lines.Columns.Add(new DataColumn("QTY", typeof(int)));
lines.Columns.Add(new DataColumn("Description", typeof(string)));

dgvQuoteLines.DataSource = lines;
dgvQuoteLines.Columns[0].Visible = false;

* 更新* 我现在已成功将组合框添加到数据网格视图,但遗憾的是数据源无法正常工作!

DataGridViewComboBoxColumn colbox = new DataGridViewComboBoxColumn();
colbox.DataPropertyName = "0";
dgvQuoteLines.Columns.Add(colbox);
c# datagridview combobox datacolumn
3个回答
5
投票

我想这就是你想要的:

DataGridViewComboBoxColumn colbox = new DataGridViewComboBoxColumn();
colbox.DataSource = products_list.ToList();
colbox.ValueMember = "Key";
colbox.DisplayMember = "Value";
dgvQuoteLines.Columns.Add( colbox );

查看 DataGridViewComboBoxColumn 类。


1
投票

colBox.DataSource = products_list.Values.ToList();

您想在组合框中显示什么?


0
投票
          string str4 = "select * from tblDepartment";
         DataGridViewComboBoxColumn colbox = new DataGridViewComboBoxColumn();
         colbox.DataSource = clsKetnoi.clsKetnoi.LayDuLieu(str4);
         colbox.Name = "Department";
         colbox.HeaderText  = "Department";
         colbox.ValueMember = "IDDepartment";
         colbox.DisplayMember = "DepartmentName";
         dgvDevice.Columns.Add(colbox);
© www.soinside.com 2019 - 2024. All rights reserved.