简体   繁体   English

C#数据绑定ComboBox自动更新

[英]C# Databound ComboBox Automatic Update

I have a combobox bound to a database table. 我有一个绑定到数据库表的组合框。 When the user inserts a new piece of data into the table, I want the combobox to be automatically updated to display this data, however I am unsure of how I would go about doing this. 当用户在表中插入新数据时,我希望组合框自动更新以显示此数据,但是我不确定如何去做。

Help would be greatly appreciated. 帮助将不胜感激。

I have something similar to this in a program of mine. 我的程序中有类似的东西。

Whenever my datagridview is updated, I empty the combobox and refill it. 每当我的datagridview更新时,我都会清空组合框并重新填充它。

Something along the lines of (I don't have my code available right now, but this is the general idea): 类似于以下内容(我现在没有可用的代码,但这是一般的想法):

if (!combobox.isempty())
{
    combobox.clear()
}

// Fill combobox here

i think, in the moment you know you add some data to this table, you should call: 我认为,在您知道向该表中添加一些数据后,应该致电:

yourcombobox.Databind();

also you must define yourcombobox.DataSource property again, before calling Databind() 另外,您必须在调用Databind()之前再次定义yourcombobox.DataSource属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM