通过自定义组合框项检索值

本文关键字:检索 组合 自定义 | 更新日期: 2023-09-27 18:33:13

我正在尝试检索博士的数据。GetInt32(0) 并将其分配到组合框选择值中,但我收到此错误。

错误 1 最佳重载方法匹配 'WindowsFormsApplication2.customComboBoxItem.customComboBoxItem(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();
        }

通过自定义组合框项检索值

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

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