如何在每次交易后(即保存数据等)更新带有数据库的表单自动完成源。我无法使用我保存的数据自动完成,直到我关闭应用程序并重新打开它。
我正在使用C#,winform,SQLite:
public DataForm() {
InitializeComponent();
AutoCompleteTextPlot();
AutoCompleteTextLocation();
AutoCompleteTextSize();
AutoCompleteTextComArea();
AutoCompleteTextRemarks();
AutoCompleteTextOwnerName();
AutoCompleteTextCustomerName();
}
答案 0 :(得分:0)
因此,在提交表单后,您似乎没有重新加载数据源。感觉有点笨拙,但您可以在成功提交后重新加载所有自动加载提示:
public DataForm()
{
InitializeComponent();
InitializeAutocomplete(() => InitializeAutocomplete(tbName, "OwnerName", "Owners"));
InitializeAutocomplete(() => InitializeAutocomplete(tbRemarks, "RemarkText", "Remarks"));
}
List<Action> autoCompleteInitializers = new List<Action>();
private void ReinitializeAutocomplete()
{
foreach(var initializer in autoCompleteInitializers)
{
initializer();
}
}
private void InitializeAutocomplete(Action init)
{
init();
autoCompleteInitializers.Add(init);
}
private void InitializeAutocomplete(TextBox tb, string fieldName, string tableName)
{
tb.AutoCompleteMode = AutoCompleteMode.Suggest;
tb.AutoCompleteSource = AutoCompleteSource.CustomSource;
tb.AutoCompleteCustomSource = new AutoCompleteStringCollection();
using (SQLiteConnection dbconn = new SQLiteConnection())
{
dbconn.Open();
using (SQLiteCommand dbcmd = dbconn.CreateCommand())
{
dbcmd.CommandText = String.Format("select {0} from {1}", fieldName, tableName);
dbcmd.CommandType = CommandType.Text;
using (SQLiteDataReader reader = dbcmd.ExecuteReader())
{
while (reader.Read())
{
tb.AutoCompleteCustomSource.Add(reader[fieldName].ToString());
}
}
}
}
}
private void btnSubmit_Click(object sender, EventArgs e)
{
if (bTransactionSuccess) ReinitializeAutocomplete();
}