2013-09-30 41 views
-1

我试图检索dr.GetInt32(0)的数据并将其分配到组合框selectedvalue,但我得到此错误。通过customComboBoxItem检索值

错误1为 'WindowsFormsApplication2.customComboBoxItem.customComboBoxItem(字符串,字符串 )' 最好的重载的方法匹配有一些无效 参数C:\用户\ bilgisayar \桌面\ WindowsFormsApplication2 \ WindowsFormsApplication2 \ Form1.cs中56 36 WindowsFormsApplication2

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace WindowsFormsApplication2 
{ 
    class customComboBoxItem 
    { 
     private string text; 
     private string value; 

     public customComboBoxItem(string strText, string strValue) 
     { 
      this.text = strText; 
      this.value = strValue; 
     } 

     public string Text 
     { 
      get { return text; } 
     } 

     public string Value 
     { 
      get { return value; } 
     } 
    } 
} 

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

using System.IO; 
using System.Data.SqlClient; 

namespace WindowsFormsApplication2 
{ 
    public partial class Form1 : Form 
    { 
     List<customComboBoxItem> customItem = new List<customComboBoxItem>(); 

     public Form1() 
     { 
      InitializeComponent(); 
      comboBox1.DataSource = customItem; 
      comboBox1.DisplayMember = "Text"; 
      comboBox1.ValueMember = "Value"; 
     } 

     string path; 

     SqlConnection cnn = new SqlConnection("Initial Catalog=randomcompany;Data Source=localhost;Integrated Security=SSPI;"); 


     private void Form1_Load(object sender, EventArgs e) 
     { 
      temizle(); 
     } 

     void temizle() 
     { 

      textBox1.Text = ""; 
      textBox2.Text = ""; 
      pictureBox1.Image = null; 
      comboBox1.SelectedIndex = -1; 
      comboBox1.Items.Clear(); 
      button1.Enabled = true; 
      button4.Enabled = false; 

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

      cnn.Close(); 
     } 

回答

2

您应该将string值传递给customComboBoxItem构造函数。 dr.GetInt32(0)返回int值,所以您应该将其投射到string

customItem.Add(new customComboBoxItem(dr.GetString(1) + dr.GetString(2), dr.GetInt32(0).ToString()));