如何将项目从WPF中的数据库添加到组合框 [英] how to add items to combobox from database in WPF

查看:71
本文介绍了如何将项目从WPF中的数据库添加到组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,

我想在加载时从数据库向组合框添加项目.

但是它不会在组合框中显示值,而是在文本框中显示.
要添加到组合框的代码是否有任何区别.

我写的代码如下:(通过引用更改的datagrid选择来检索值)

Sir,

I want to add items to combobox from database at loading time.

But it doesn''t showing the values in the combobox, but in the textbox it is showing.
Is there any differnce in code to add to the combobox.

I wrote the code as follows: (values are retreiving by refering a datagrid selection changed)

private void employeemasterdg_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    try
    {
        System.Collections.IList selectedUser = e.AddedItems;
        txtcode.Text = ((DataRowView)selectedUser[0]).Row.ItemArray[0].ToString();
        txtname.Text = ((DataRowView)selectedUser[0]).Row.ItemArray[1].ToString();
    }
    catch (Exception e1)
    {
        System.Windows.MessageBox.Show(e1.Message);
    }
    conn.Close();
    SqlDataReader rd3 = null;
    try
    {
        System.Collections.IList selectedUser = e.AddedItems;
        SqlCommand cmd3 = new SqlCommand();
        conn.Open();
        cmd3.Connection = conn;
        String str3 = "Select email,branch,department from empmaster where empcode=@ccode3";
        cmd3.Parameters.Add(new SqlParameter("ccode3", ((DataRowView)selectedUser[0]).Row.ItemArray[0].ToString()));
        cmd3.CommandText = str3;
        rd3 = cmd3.ExecuteReader();

        while (rd3.Read())
        {
            //HERE IS THE PROBLEM
            this.txtemail.Text = rd3.GetString(0);  //THIS DISPLAYS
            this.cmbtest.Text = rd3.GetString(1);  //THIS DOESN'T DISPLAY-THIS IS A COMBOBOX
            this.cmbdept.Text = rd3.GetString(2);  //THIS ALSO DOESN'T DISPLAY
            System.Windows.MessageBox.Show(rd3.GetString(1));  //THIS DISPLAYS
        }
    // ...
    }
    catch
    {
        // ...
    }
// ...
}


注意:它(要显示的项目)已经在组合框中.在WPF中,它没有向组合框显示属性SelectedText,而是显示SelectedItem.
但是我已经给出了SelectedItem.然后也没有显示.


NB: It(the item which want to display) is in the combobox already. In WPF it is not showing a property SelectedText to the combobox, instead it is showing SelectedItem.
But I already had given as SelectedItem. Then also it didn''t display.

推荐答案

LukmanulHakeem.T写道:
LukmanulHakeem.T wrote:

this.cmbtest.Text = rd3.GetString(1);//不显示-这是一个组合框
this.cmbdept.Text = rd3.GetString(2);//这也不显示

this.cmbtest.Text = rd3.GetString(1);//THIS DOESN''T DISPLAY-THIS IS A COMBOBOX
this.cmbdept.Text = rd3.GetString(2);//THIS ALSO DOESN''T DISPLAY



改为设置SelectedText,并确保它已在组合框中.



Set the SelectedText instead, and make sure it''s in the combobox already.


这篇关于如何将项目从WPF中的数据库添加到组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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