花了几个小时,可以了,CellValueNeeded事件,提供DataGridViewComboboxCell.value的值时,
之前我以为应该提供,DispayMember的值,但这样引以异常,百思不得其解,看了N次MSDN,也没有找到
这样的使用场景,在网上,发现很多朋友都碰到这个问题,但没有发现解决的方法,其实是我们的潜意识影响
了我们。因为平常遇到问题,在网上找到了大家提供的一些信息。说实话,这是我的第一次写这东西,希望
能对遇上这个问题的你解决这个问题,好像MSDN,上有这方面的描述,可可能是我理解方面的问题,不清楚
它说得就是这个意思
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
List<int,string> l = new List<int,string>();
l.Add(new test(1, "aa"));
l.Add(new test(2, "bb"));
this.Column1.DataSource = l;
this.Column1.DisplayMember = "Value";
this.Column1.ValueMember = "Key";
this.dataGridView1.RowCount = 2;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void dataGridView1_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
{
e.Value = 2;//这里给的值要是ValueMember中有的数据,这样DataGridViewComboboxCell显示的值就为
//它相应的DisplayMember 值
}
}