插入文本给objectcollection错误 [英] Inserting text gives objectcollection error

查看:125
本文介绍了插入文本给objectcollection错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要插入每一行SQL到组合框,其中雇员将组合框的值,并EmployeeFirstName EmployeeLastName将组合框项目文本。然而,这行



给我这个错误:




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



解决方案

定义新类

 公共类EmpItem 
{
公众诠释EMPID;
公共字符串empName;
}



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

 无效comboboxrefresh()
{
comboBox1的的DisplayMember和ValueMember .DisplayMember =empName;
comboBox1.ValueMember =EMPID;
cnn.Open();
的SqlCommand CMD =新的SqlCommand(选择雇员,EmployeeFirstName,EmployeeLastName FROM员工,美国有线电视新闻网)
SqlDataReader的博士= cmd.ExecuteReader();
如果(dr.HasRows)
{
,而(dr.Read())
{
EmpItem EI =新EmpItem(){EMPID = dr.GetInt32( 0),empName = dr.GetString(1)+ dr.GetString(2)};
comboBox1.Items.Add(EI);
}
}
cnn.Close();
}


I want to insert each row in SQL into combobox, where EmployeeID will be combobox Value, and EmployeeFirstName EmployeeLastName will be text of combobox item. However this line

gives me this error:

Error 1 The best overloaded method match for 'System.Windows.Forms.ComboBox.ObjectCollection.Insert(int, object)' has some invalid arguments C:\Users\bilgisayar\Desktop\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 45 21 WindowsFormsApplication1

解决方案

Define a new class

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

While reading the DataReader create an instance of this class and add it to the combobox items collections. Do not forget to set the DisplayMember and ValueMember of the combobox

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

这篇关于插入文本给objectcollection错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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