【问题标题】:combobox data bind (C# winforms)组合框数据绑定(C# winforms)
【发布时间】:2017-11-09 13:32:10
【问题描述】:

这段代码的问题在哪里?当我启动程序时,comboBox 中没有可供选择的值。编译和启动应用程序没有问题。我不知道这里有什么问题。也许有人可以解决这个问题。

链接到 pastebin https://pastebin.com/pASVNWq

using MySql.Data.MySqlClient;
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;

namespace parKing_new
{
    public partial class editClient : Form
    {
        public editClient()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {}

    //Load customer ID to a combobox
        private void LoadCustomersId()
        {
            var connectionString = 
"Server=localhost;Port=3306;Database=ewisys;Uid=root;password=;";
            using (var connection = new MySqlConnection(connectionString))
            {
                connection.Open();
                var query = "SELECT clientID FROM clients";
                using (var command = new MySqlCommand(query, connection))
                {
                    using (var reader = command.ExecuteReader())
                    {
                        //Iterate through the rows and add it to the 
combobox's items
                        while (reader.Read())
                        {
                        comboBox1.Items.Add(reader.GetString("clientID"));
                        }
                    }
                }
            }
       }

        //Load customer details using the ID
        private void LoadCustomerDetailsById(int id)
        {
            var connectionString = 
"Server=localhost;Port=3306;Database=ewisys;Uid=root;password=;";
            using (var connection = new MySqlConnection(connectionString))
           {
               connection.Open();
               var query = "SELECT clientID, name, surName FROM clients WHERE 
Id = @clientID";
                using (var command = new MySqlCommand(query, connection))
                {
                //Always use SQL parameters to avoid SQL injection and it 
automatically escapes characters
                    command.Parameters.AddWithValue("@clientID", id);
                    using (var reader = command.ExecuteReader())
                    {
                        //No customer found by supplied ID
                        if (!reader.HasRows)
                            return;

                        ClientIDTextBox.Text = 
                    reader.GetInt32("clientID").ToString();
                    nameTextBox.Text = reader.GetString("name");
                    surNameTextBox.Text = reader.GetString("surName");
                    }
                }
           }
       }

      private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var clientID = Convert.ToInt32(comboBox1.Text);
            LoadCustomerDetailsById(clientID);
        }
   }
}

【问题讨论】:

  • 有什么问题
  • 你在某个时候打电话给LoadCustomersId吗?您应该添加Load 事件并在该事件上调用LoadCustomersId
  • 请在调试时展示您的努力。

标签: c# mysql winforms data-binding combobox


【解决方案1】:

我认为您只定义了 LoadCustomersId() 方法,但您没有从任何地方调用它。您需要调用该方法

【讨论】:

    【解决方案2】:

    将此添加到您的代码中。

    这背后的原因是,当您启动应用程序时,没有调用 LoadCustomersId() 函数来填充组合框上的数据。所以使用 Load 事件处理程序并从那里填充您的组合框:

    private void editClient_Load(object sender, EventArgs e)
        {
           LoadCustomersId();
        }
    

    【讨论】:

    • 好的,谢谢,现在我可以在组合框中看到 ID,但是当我单击任何索引时,我看不到文本框中的值。你也可以帮我解决这个问题吗?我的目标是从组合框中获取客户端,下一步将是从数据库中删除记录的按钮。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 2011-08-25
    • 1970-01-01
    • 2010-11-22
    • 2013-12-24
    • 1970-01-01
    相关资源
    最近更新 更多