插入文本会导致对象集合错误

本文关键字:集合 错误 对象 插入文本 | 更新日期: 2023-09-27 18:33:09

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

给我这个错误:

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

插入文本会导致对象集合错误

定义一个新类

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

读取 DataReader 时,创建此类的实例并将其添加到组合框项集合中。不要忘记设置组合框的显示成员和值成员

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();
}

试试这个

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));

我想你正在寻找数据源

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();
}

首先,使用 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;

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));