0

我想将 SQL 中的每一行插入组合框,其中 EmployeeID 将是组合框值,而 EmployeeFirstName EmployeeLastName 将是组合框项的文本。然而这条线

给我这个错误:

错误 1 ​​'System.Windows.Forms.ComboBox.ObjectCollection.Insert(int, object)' 的最佳重载方法匹配有一些无效参数 C:\Users\bilgisayar\Desktop\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 45 21 WindowsFormsApplication1

4

4 回答 4

2

定义一个新类

public class EmpItem
{
   public int empID;
   public string empName;
}  

在读取 DataReader 时,创建此类的一个实例并将其添加到组合框项目集合中。不要忘记设置组合框的 DisplayMember 和 ValueMember

void comboboxrefresh()
{
    comboBox1.DisplayMember = "empName";
    comboBox1.ValueMember = "empID";
    cnn.Open();
    SqlCommand cmd = new SqlCommand("SELECT EmployeeID,EmployeeFirstName,EmployeeLastName FROM Employees", cnn);
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr.HasRows)
    {
        while (dr.Read())
        {
            EmpItem ei = new EmpItem() { empID=dr.GetInt32(0), empName = dr.GetString(1) + dr.GetString(2)};
            comboBox1.Items.Add(ei);
        }
    }
    cnn.Close();
}
于 2013-09-27T19:21:04.603 回答
1

首先,使用Add将项目添加到ComboBox. 其次,我更喜欢在表单中立即anonymous type填写ComboBox您使用纯原生Sql和简单的内容。DataReader

void comboboxrefresh()
    {
        cnn.Open();
        SqlCommand cmd = new SqlCommand("SELECT EmployeeID,EmployeeFirstName,EmployeeLastName FROM Employees", cnn);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            combobox1.ValueMember = "Id";
            combobox1.DisplayMember = "FullName";

            while (dr.Read())
            {
                comboBox1.Items.Add(
                  new {
                         FullName = dr.GetString(1) + " " + dr.GetString(2), 
                         Id = dr.GetInt32(0)
                      });
            }
        }

        cnn.Close();
    }

更新:

我注意到天真地添加到Item列表中是行不通的!相反,应该将DataSource属性设置为所需的列表。ComboBox所以工作代码如下:

var list = new List<object>();
combobox1.ValueMember = "Id";
combobox1.DisplayMember = "FullName";
while (dr.Read())
{
   list.Add(
       new {
              FullName = dr.GetString(1) + " " + dr.GetString(2), 
              Id = dr.GetInt32(0)
            });
 }
combobox1.DataSource = list;
于 2013-09-27T19:29:48.143 回答
1

尝试这个

comboBox1.Items.Insert(index, dr.GetString(1) + dr.GetString(2));

index您要在组合框中插入的整数值在哪里

你是说这个吗

comboBox1.Items.Insert(dr.GetInt32(0), dr.GetString(1) + dr.GetString(2));

如果您只想添加组合框,请尝试以下操作

comboBox1.Items.Add(dr.GetString(1) + dr.GetString(2));

我认为您正在寻找DataSource

void comboboxrefresh()
{
    cnn.Open();
    SqlCommand cmd = new SqlCommand("SELECT EmployeeID, (EmployeeFirstName + EmployeeLastName) as EmployeeName FROM Employees", cnn);
    DataTable table = new Datatable();
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    adapter.Fill(table);

    comboBox1.DisplayMember = "EmployeeName";
    comboBox1.ValueMember = "EmployeeID";

    comboBox1.DataSource = table;

    cnn.Close();
}
于 2013-09-27T19:20:55.060 回答
0

Items.Insert正在寻找您在 ComboBox 列表中的特定点插入。

你想用Items.Add

comboBox1.Items.Add(dr.GetString(1) + dr.GetString(2) + dr.GetInt32(0));

如果你想使用Insert它需要是这样的:

comboBox1.Items.Insert(0, dr.GetString(1) + dr.GetString(2) + dr.GetInt32(0));
于 2013-09-27T19:23:29.770 回答