postgresql ComboBox.ValueMember和DisplayMember

noj0wjuj  于 2023-08-04  发布在  PostgreSQL
关注(0)|答案(9)|浏览(120)

如何设置这些值?我有一个DataTable,里面有我想在组合框中设置的所有数据,但我找不到如何设置它。
我试过了

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

字符串
没有编译错误,警告,什么都没有。就跳出来了
这是用于填充DataTable的查询

"Select * from \"Table\""


我检查了调试器,数据表已填充。列名为“id”和“name”。ComboBox为空。我第一次把它填满!

xv8emn3q

xv8emn3q1#

您不应该按此顺序设置列表框和/或组合框的datasource

ComboBox1.DataSource = dataTable;

ComboBox1.ValueMember = "id"; 

ComboBox1.DisplayMember = "name";

字符串

相反,这是正确的顺序:

ComboBox1.ValueMember = "id"; 

ComboBox1.DisplayMember = "name";

ComboBox1.DataSource = dataTable;


注:设置datasource应在最后一行。
如果您先设置datasourceSelectedIndexChanged事件将触发,您可能会收到强制转换错误或其他异常。

vxf3dgd4

vxf3dgd42#

使用键值对填充组合框

填充组合框的一个简洁的方法是将数据源设置为一个键值对列表。它还可以激发使用存储在某种列表中的数据:

//Some values to show in combobox
string[] ports= new string[3] {"COM1", "COM2", "COM3"};

//Set datasource to string array converted to list of keyvaluepairs
combobox.Datasource = ports.Select(p => new KeyValuePair<string, string>(p, p)).ToList();

//Configure the combo control
combobox.DisplayMember = "Key";
combobox.ValueMember = "Value";
combobox.SelectedValue = ports[0];

字符串
也可以使用以下语法填充数据源:

ports.Select(p => new { Key = p, Value = p }).ToList();


该技术可以用多个列列表的更多属性名称来扩展。
可以直接使用已经是键值对的对象(如Dictionary项)

combobox.DataSource = new Dictionary<int, string>()
{
    {0, "COM1"},
    {1, "COM2"},
    {2, "COM3"},
}.ToList();
combobox.ValueMember = "Key";
combobox.DisplayMember = "Value";


元组可以像这样初始化和使用

var ports= new List<Tuple<int, string>>()
{
    Tuple.Create(0, "COM1"),
    Tuple.Create(1, "COM2"),
    Tuple.Create(2, "COM3")
};

combobox.DataSource = ports;
combobox.ValueMember = "Item1";
combobox.DisplayMember = "Item2";

qlzsbp2j

qlzsbp2j3#

ComboBox1.DataSource= dt; //the data table which contains data
  ComboBox1.ValueMember = "id";   // column name which you want in SelectedValue
  ComboBox1.DisplayMember = "name"; // column name that you need to display as text

字符串

dwbf0jvd

dwbf0jvd4#

他们用绳子…

ComboBox1.ValueMember = "id"; 
ComboBox1.DisplayMember = "name";

字符串

avwztpqn

avwztpqn5#

我也有同样的问题。在我的例子中,SelectedIndexChanged事件触发并跳出该方法。尝试不要使用SelectedIndexChanged事件。或者类似这样的东西:

ComboBox1.SelectedIndexChanged -= new System.EventHandler(ComboBox1_SelectedIndexChanged); 
ComboBox1.DataSource = dataTable; 
ComboBox1.ValueMember = "id";  
ComboBox1.DisplayMember = "name";
ComboBox1.SelectedIndexChanged += new System.EventHandler(ComboBox1_SelectedIndexChanged);

字符串
对我很有效。=)

at0kjp5o

at0kjp5o6#

ComboBox1.ValueMember = dataTable.Columns["id"].ColumnsName;   // column name which the values are not visible 
ComboBox1.DisplayMember = dataTable.Columns ["name"].ColumnsName;
 /* 
       column name that you need to select item by proprity : 
ComboBox1.SelectedItem; 
Or   you can use easly this : 
ComboBox1.Text; 
*/

ComboBox1.DataSource= dataTable; //the data table which contains data 
// and this should be last :)

字符串

72qzrwbm

72qzrwbm7#

public class ComboDeger {
    private string yazi;
    private int deger;
    public ComboDeger(string stryazi, int strdeger) {
        this.yazi = stryazi;
        this.deger = strdeger;
    }
    public string yazisi {
        get {
            return yazi;
        }
    }
    public int degeri {
        get {
            return deger;
        }
    }
}
private void combobox_doldur() {
    ArrayList ComboDegerleri = new ArrayList();
    ComboDegerleri.Add(new ComboDeger("9 : NORMAL", 9));
    ComboDegerleri.Add(new ComboDeger("10 : ENGELLİ", 10));
    comboBox1.DataSource = ComboDegerleri;
    comboBox1.DisplayMember = "yazisi";
    comboBox1.ValueMember = "degeri";
}
private void Form3_Load(object sender, EventArgs e) {
    con.Open();
    combobox_doldur();

    // Populate the COMBOBOX using an array as DataSource.
}

字符串

f0ofjuux

f0ofjuux8#

你可以这样指定

ComboBox1.ValueMember = "id";  
  ComboBox1.DisplayMember = "name";

字符串

xeufq47z

xeufq47z9#

也许不完全是所描述的问题,但在我的情况下,类型名称显示在组合框中,而不是显示值,尽管一切似乎都连接正确。结果发现,类型的公共属性是在没有getter和setter的情况下定义的**!:)

相关问题