-1

我正在尝试检索 dr.GetInt32(0) 的数据并将其分配到组合框 selectedvalue 中,但出现此错误。

错误 1 ​​'WindowsFormsApplication2.customComboBoxItem.customComboBoxItem(string, string)' 的最佳重载方法匹配有一些无效参数 c:\users\bilgisayar\Desktop\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();
        }
4

1 回答 1

2

您应该将string值传递给customComboBoxItem构造函数。dr.GetInt32(0)返回int值,因此您应该将其转换为string.

customItem.Add(new customComboBoxItem(dr.GetString(1) + dr.GetString(2), dr.GetInt32(0).ToString()));
于 2013-09-30T03:38:52.830 回答