datagridview的数据源与和ComboBox

datagridview的数据源与和ComboBox

本文介绍了datagridview的数据源与和ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个连接到它的数据源中的DataGridView制成的自定义数据表:



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



在DataGridView是可编辑的,但是,对于列0我需要显示的组合框而不是文本字段。我如何去实现这个

 内部字典< INT,串> products_list =新词典< INT,串>(); 
products_list.Add(0,试验产品1);
products_list.Add(1,试验产品2);


lines.Columns.Add(新的DataColumn(产品,products_list.GetType()));
lines.Columns.Add(新的DataColumn(数量的typeof(INT)));
lines.Columns.Add(新的DataColumn(说明中的typeof(字符串)));

dgvQuoteLines.DataSource =线;
dgvQuoteLines.Columns [0] =。可见假的;



* UPDATE *
现在我已成功地添加组合框到DataGridView但可悲的是数据源不工作

  DataGridViewComboBoxColumn colbox =新DataGridViewComboBoxColumn()!; 
colbox.DataPropertyName =0;
dgvQuoteLines.Columns.Add(colbox);


解决方案

我想这是你想要的>

  DataGridViewComboBoxColumn colbox =新DataGridViewComboBoxColumn(); 
colbox.DataSource = products_list.ToList();
colbox.ValueMember =键;
colbox.DisplayMember =值;
dgvQuoteLines.Columns.Add(colbox);


I have a datagridview with a datasource attached to it made of a custom datatable:

DataTable: 0 = Dictionary 1 = string 2 = string

the datagridview is editable, however for column 0 I need to show a combobox instead of a text field. How do I go about achieving this?

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;

* UPDATE *I have now managed to add the combobox to the datagridview but sadly the datasource isn't working!

DataGridViewComboBoxColumn colbox = new DataGridViewComboBoxColumn();
colbox.DataPropertyName = "0";
dgvQuoteLines.Columns.Add(colbox);
解决方案

I think this is what you want:

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

这篇关于datagridview的数据源与和ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:35