DataGridView的组合框小区C# [英] datagridview combobox cell c#

查看:97
本文介绍了DataGridView的组合框小区C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

 DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell();     
 DataTable data = new DataTable();

data.Columns.Add(new DataColumn("Value", typeof(string)));
data.Columns.Add(new DataColumn("Description", typeof(string)));



data.Rows.Add("5", "6");
data.Rows.Add("51", "26");
data.Rows.Add("531", "63");
cell.DataSource = data;
cell.ValueMember = "Value";
cell.DisplayMember = "Description";

cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
dataGridView1.Rows[0].Cells[0] = cell;



它显示组合框,但无法选择它的任何价值。
哪些错误

It displays combobox but can't select any value of it. Whats wrong

推荐答案

您一般不与个人的细胞类型的工作 DataGridView的。而是要添加键入 DataGridViewComboBoxColumn 中的列

You generally don't work with the individual cell types in the DataGridView. Instead you want to add a column of type DataGridViewComboBoxColumn.

因此,而不是你提供的代码,你想是这样的:

So instead of your provided code you want something like:

var column = new DataGridViewComboBoxColumn();      
DataTable data = new DataTable(); 

data.Columns.Add(new DataColumn("Value", typeof(string))); 
data.Columns.Add(new DataColumn("Description", typeof(string))); 

data.Rows.Add("5", "6"); 
data.Rows.Add("51", "26"); 
data.Rows.Add("531", "63"); 

column.DataSource = data; 
column.ValueMember = "Value"; 
column.DisplayMember = "Description"; 

dataGridView1.Columns.Add(column); 

有关参考,在DataGridViewCombobBoxCell的文件是MSDN上的此处。您还可以找到一般来说有DataGridView的信息。另一个很好的参考是 DataGridViewFAQ

For reference, the documentation on the DataGridViewCombobBoxCell is on MSDN here. You can also find information on the DataGridView in general there. Another very good reference is the DataGridViewFAQ.

这篇关于DataGridView的组合框小区C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆