用于WFA的新ListItem

时间:2013-09-30 04:03:09

标签: c# winforms listbox .net-4.5

我在下面的代码中非常适合ASP.NET。

然而,当我尝试在WinForms .NET 4.5中使用它时,它给了我

错误1找不到类型或命名空间名称'ListItem'(您是否缺少using指令或程序集引用?)

    cnn.Open();
    SqlCommand cmd = new SqlCommand("SELECT EmployeeID,EmployeeFirstName,EmployeeLastName FROM Employees", cnn);
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr.HasRows)
    {
        while (dr.Read())
        {
            listBox1.Items.Add(new ListItem(dr.GetString(1) + dr.GetString(2), dr.GetInt32(0).ToString()));
        }
    }

    cnn.Close();

1 个答案:

答案 0 :(得分:1)

Windows窗体中不存在ListItem

只需将项目添加到ListBox,如下所示:

    SqlCommand cmd = new SqlCommand("SELECT EmployeeID,EmployeeFirstName,EmployeeLastName FROM Employees", cnn);
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr.HasRows)
    {
        while (dr.Read())
        {
            listBox1.Items.Add(dr.GetString(1) + dr.GetString(2) +
                                         dr.GetInt32(0).ToString());
        }
    }

ListBox.Items.Add() documentation