简体   繁体   English

将数据读取到数组,然后在C#中填充到ComboBox

[英]Reading data to an Array, then populating to a ComboBox in C#

I have user data (name, username, image) in a database and I need to list the name and id in a combo box. 我在数据库中有用户数据(名称,用户名,图像),我需要在组合框中列出名称和ID。 When an item in the combo box is selected it should display a picture. 当在组合框中选择一个项目时,它应该显示一张图片。 I have created a User and a UserDB class. 我创建了一个User和一个UserDB类。 In my User class I have the following code: 在我的User类中,我有以下代码:

public class User
  {
    private String u_firstname;
    private String u_lastname;
    private String u_username = string.Empty;
    private Byte [] u_image;
    private Byte [] u_template;


    public User(OleDbDataReader dr)
    {            
        u_firstname = dr["USER_FIRST_NM"].ToString();
        u_lastname = dr["USER_LAST_NM"].ToString();
        u_username = dr["USER_NAME"].ToString();
        u_image = (Byte [])dr["USER_IMAGE"];
        u_template = (Byte [])dr["USER_TEMPLATE"];

    }
}

In my UserDB class I have the following code: 在我的UserDB类中,我有以下代码:

public class UserDB
 {
    public static String workingDirectory = System.IO.Directory.GetCurrentDirectory();
    public static String dbProvider = "Microsoft.ACE.OLEDB.12.0";
    public static String dbName = "Users_DB.accdb";        
    public static String dbConnString = String.Format(@"Provider={0};Data Source={1}\Data\{2}", dbProvider, workingDirectory, dbName);


    public User[] usersInDatabaseList()
    {
        User u;
        List<User> clist = new List<User>();

        OleDbConnection cn = new OleDbConnection(dbConnString);

        String strSQL = "SELECT USER_FIRST_NM, USER_LAST_NM, USER_NAME, USER_IMAGE, USER_TEMPLATE FROM KF001_USERS";

        OleDbCommand cm = new OleDbCommand(strSQL, cn);
        OleDbDataReader dr;

        cn.Open();

        dr = cm.ExecuteReader(CommandBehavior.CloseConnection);

        while (dr.Read())
        {                
            u = new User(dr);
            clist.Add(u);
        }

        cn.Close();


        return clist.ToArray();
    }
}

and on my mainForm I have the following method that I am using for my combo-box: 在我的mainForm上,我有以下用于组合框的方法:

public void FillDropDownList()
    {
        try
        {

            //int i;
            UserDB db = new UserDB();
            User[] userList = db.usersInDatabaseList();

            comboBoxUsersEnrolled.Items.AddRange(userList);
            comboBoxUsersEnrolled.DisplayMember = ?      
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
}

I am at a total loss as to how to get my database values to be displayed in the combobox. 我完全不知道如何在组合框中显示我的数据库值。 I'm also unsure of how to display a picture when an Item is selected. 我也不确定选择项目时如何显示图片。 Can anyone help or steer me in the right direction please. 任何人都可以帮助或指导我正确的方向。

So I changed filldropdownlist method to the following: 因此,我将filldropdownlist方法更改为以下内容:

 public void FillDropDownList()
    {

            UserDB db = new UserDB();
            User[] userList = db.cList();



            for (int i = 0; i < userList.Length; i++)
            {

                comboBoxUsersEnrolled.Items.Add(userList[i]);
                comboBoxUsersEnrolled.DisplayMember = "ComboName";




                if (comboBoxUsersEnrolled.SelectedIndex > 0 )
                {

                    byte[] pictureByteReader = (userList[i].UserImage);
                    MemoryStream ms = new MemoryStream(pictureByteReader);
                    Image picture = Image.FromStream(ms);
                    picBoxCapture.Image = picture;
                }

            }


    }

I'm trying to add to my combobox and at the same time send a picture to a picturebox if that item is selected. 我正在尝试将其添加到我的组合框,同时如果选择了该项目,则同时将图片发送到图片框。 My code does nothing. 我的代码什么都不做。 Gives no errors or anything. 没有任何错误或任何提示。 Help! 救命!

You have done most of the work already. 您已经完成了大部分工作。

The DisplayMember property that you've marked with ? 您已标记的DisplayMember属性? indicates the property that should be used to display in the ComboBox. 指示应用于在ComboBox中显示的属性。 Your class doesn't have any properties - it only has fields - so first you'll want to create a property 您的课程没有任何属性-它仅包含字段-因此,您首先要创建一个属性

public class User
{
    // all your existing fields

    public string Name
    {
        get
        {
            return this.u_username;
        }
    }

Then you can set the DisplayMember = "Name"; 然后可以设置DisplayMember = "Name"; to use the Name as the display property. 使用名称作为显示属性。

Using an image requires more work - you will need to use a template in the combobox instead of a string. 使用图像需要做更多的工作-您将需要在组合框中使用模板而不是字符串。 Try googling that to find an example. 尝试使用Google搜索来查找示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM