[不是问题] C#Refresh Combo Box [英] [Not a question] C# Refresh Combo Box

查看:115
本文介绍了[不是问题] C#Refresh Combo Box的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组合框,其数据来自数据库。在向数据库添加新项目以及编辑和删除项目时遇到问题。当我执行这些操作时,组合框在应用程序重新启动之前不会更新。我只是一名学生,我需要帮助。我通过Visual Studio 2010中提供的选项限制了数据。

I have a combo box with data bounded from a Data Base. I am having problems when adding new items to database and also editing and deleting items. When I do those actions, The combobox is not getting updated until the app restart. I am just a student and I need help for this. I bounded the data through the option available in Visual Studio 2010.

推荐答案

如果你在表单中加入一个刷新按钮,它可以是一个很好的解决方案查询你的数据库并将数据重新绑定到你的组合框。
It can be a good solution if you put into your form a refresh button and with it you can make a query for your database and rebind the data to your combobox.


只需用Google搜索2秒就可以找到:

Just googled for 2 seconds and found this:
foreach (Control ctl in this.Controls)
{
    ctl.ResetBindings();
}


感谢所有花时间提供帮助的人。即使我没有得到答案,我找到了这样做的方法。我会把它作为帮助发给我这样的另一个初学者。



SqlConnection con = new SqlConnection(@PATH);

con .Open();

SqlCommand cmd = new SqlCommand(从TableName中选择字段,con);

SqlDataAdapter SDA = new SqlDataAdapter();

SDA.SelectCommand = cmd;

DataTable DT = new DataTable();

SDA.Fill(DT);

comboBox1。 DataSource = DT;

con.Close();
Thanks for all who spent their time to help. Even I didn't get the answer, I found the way to do so. I will post it as a help to another beginner like me.

SqlConnection con = new SqlConnection(@"PATH");
con.Open();
SqlCommand cmd = new SqlCommand("select Field from TableName", con);
SqlDataAdapter SDA = new SqlDataAdapter();
SDA.SelectCommand = cmd;
DataTable DT = new DataTable();
SDA.Fill(DT);
comboBox1.DataSource = DT;
con.Close();


这篇关于[不是问题] C#Refresh Combo Box的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆