[英]How to fill combobox in Windows Forms c#
I try to fill one combobox with MySQL database, but I have a problem. 我尝试用MySQL数据库填充一个组合框,但出现问题。 I think everything is OK and I see my old project how to fill the comboboxes.
我认为一切都很好,我看到了我的旧项目如何填充组合框。 I think all code is the same but I forgot how exactly is filling.
我认为所有代码都是相同的,但是我忘记了填充的确切程度。
So, this is my code, but combobox is not filled. 因此,这是我的代码,但组合框未填充。
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 MySql.Data.MySqlClient;
using System.Data.SqlClient;
using System.Data.Common;
using SR.Service;
namespace SR
{
public partial class allWorkers : Form
{
private WorkerService service;
MySqlConnection connection;
string MyConnectionString = "Server=localhost;Database=SR_database;Uid=root;Pwd='';";
public allWorkers()
{
InitializeComponent();
connection = new MySqlConnection(MyConnectionString);
service = new WorkerService();
}
private void btn_Main6_Click(object sender, EventArgs e)
{
this.Hide();
var Main = new Main();
Main.Show();
}
private void btn_createWorker_Click(object sender, EventArgs e)
{
//MySqlConnection connection = new MySqlConnection(MyConnectionString);
MySqlCommand cmd;
connection.Open();
cmd = connection.CreateCommand();
cmd.CommandText = service.CreateWorker(textBox1.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Вие създадохте нов работник с име: " + textBox1.Text);
MySqlCommand command = new MySqlCommand(service.GetAllWorkers(), connection);
MySqlDataAdapter da = new MySqlDataAdapter(command);
using (DataTable dt = new DataTable())
{
da.Fill(dt);
dataGridView1.DataSource = dt;
}
connection.Close();
}
private void allWorkers_Load(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void btn_deleteWorker_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MySqlCommand cmd;
connection.Open();
cmd = connection.CreateCommand();
cmd.CommandText = service.selectOnlyWorkerName();
cmd.ExecuteNonQuery();
MySqlCommand command = new MySqlCommand(service.selectOnlyWorkerName(), connection);
using (var reader = command.ExecuteReader())
{
SuspendLayout();
while (reader.Read())
{
comboBox1.Items.Add(reader["worker_name"]);
}
ResumeLayout();
}
//
//
// MySqlCommand command = new MySqlCommand(service.selectOnlyWorkerName(), connection);
// MySqlDataAdapter da = new MySqlDataAdapter(command);
//
// using (DataTable dt = new DataTable())
// {
// da.Fill(dt);
// foreach (DataRow dr in dt.Rows)
// {
// comboBox1.Items.Add(dr["worker_name"]);
// }
// }
// connection.Close();
}
}
}
I think I forgot something, but I don't know what I forgot to write. 我想我忘记了一些东西,但是我不知道自己忘记写什么。 Thank you guys.
感谢大伙们。
You need to do this, 你需要这样做
MySqlDataAdapter da = new MySqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
cb.Items.Add(dr["yourcol"]);
}
First of all, Always when you're about to change something within UI with WinForms consider wrapping this around SuspendLayout()
and ResumeLayout()
. 首先,始终要使用WinForms在UI中进行更改时,请考虑将其包装在
SuspendLayout()
和ResumeLayout()
周围。
Another thing ... have you ever consider executing your command? 另一件事...您是否考虑过执行命令? Just not to use
SqlDataAdapter
which would make your work easier. 只是不要使用
SqlDataAdapter
,这会使您的工作更加轻松。
example code: 示例代码:
MySqlCommand command = new MySqlCommand(service.selectOnlyWorkerName(), connection);
using (var reader = command.ExecuteReader())
{
SuspendLayout();
while(reader.Read())
{
comboBox1.Items.Add(reader["worker_name"]);
}
ResumeLayout();
}
or in case you really need to use SqlDataAdapter
: 或者如果您确实需要使用
SqlDataAdapter
:
MySqlCommand command = new MySqlCommand(service.selectOnlyWorkerName(), connection);
MySqlDataAdapter da = new MySqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds,"workers");
SuspendLayout();
foreach ( var row in ds.Tables["workers"].Rows)
{
comboBox1.Items.Add(row[0]);
}
ResumeLayout();
The code posted above should work perfectly fine and in case it won't then debug if your command actually has some results. 上面发布的代码应该可以正常工作,以防万一您的命令确实产生了一些结果,然后再进行调试。
I believe you should have handled the DropDown event, rather than SelectedIndexChanged. 我相信您应该已经处理了DropDown事件,而不是SelectedIndexChanged事件。
DropDown has worked for me in the past. DropDown过去为我工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.