ComboBox.ValueMember和DisplayMember [英] ComboBox.ValueMember and DisplayMember

查看:372
本文介绍了ComboBox.ValueMember和DisplayMember的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置此值?我有一个DataTable的所有数据我想在组合框中设置,但我不能找到如何设置它。

How do i set this values? I have a DataTable with all the data i want to set in the combobox, but i cant find how to set it.

我试过

ComboBox1.DataSource = dataTable;
ComboBox1.ValueMember = "id"; // --> once hes here, he just jumps out the method
ComboBox1.DisplayMember = "name";

没有编译错误,警告,没有...只是跳出来了!

No compilation error, warning, nothing.. just jumps out!

这是填充DataTable的查询

This is the query to fill the DataTable

"Select * from \"Table\""

我用调试器检查并填充了datatable。列名称为id和name。 ComboBox为空。

I checked with the debugger and the datatable was filled. The columns names are "id" and "name". ComboBox is blank. I'm filling it for the first time!

推荐答案

您不应该设置 datasource

ComboBox1.DataSource = dataTable;

ComboBox1.ValueMember = "id"; 

ComboBox1.DisplayMember = "name";

这是正确的顺序:

ComboBox1.ValueMember = "id"; 

ComboBox1.DisplayMember = "name";

ComboBox1.DataSource = dataTable;

注意:设置 datasource 应该是最后一行。

NOTE: setting datasource should be last line.

如果您先设置 datasource SelectedIndexChanged 将会触发,您可能会收到投射错误或其他异常。

If you set datasource first, SelectedIndexChanged event will fire and you may get the cast error or other exception.

这篇关于ComboBox.ValueMember和DisplayMember的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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