Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使绑定的winforms控件在其数据源更新时刷新?_C#_Winforms_Data Binding_Sql Server Ce - Fatal编程技术网

C# 如何使绑定的winforms控件在其数据源更新时刷新?

C# 如何使绑定的winforms控件在其数据源更新时刷新?,c#,winforms,data-binding,sql-server-ce,C#,Winforms,Data Binding,Sql Server Ce,我对在C#中使用数据库(但不是C#本身)以及数据绑定的整个概念非常陌生,所以请记住这一点。如果这会影响到这一点,我也在使用SQLServerCE 我尝试使用VS2010构建的强类型数据集访问SQLServerCE数据库,并将WinForms控件绑定到该数据库以显示数据。我可以很好地绑定控件,并且它们显示数据库中当前的数据没有问题。我还可以向数据库中插入新数据,如果我重新启动应用程序,插入的数据将显示在控件中(特别是一个组合框,但我将在将来使用更多) 问题是,在我重新启动应用程序之前,新行根本不会

我对在C#中使用数据库(但不是C#本身)以及数据绑定的整个概念非常陌生,所以请记住这一点。如果这会影响到这一点,我也在使用SQLServerCE

我尝试使用VS2010构建的强类型数据集访问SQLServerCE数据库,并将WinForms控件绑定到该数据库以显示数据。我可以很好地绑定控件,并且它们显示数据库中当前的数据没有问题。我还可以向数据库中插入新数据,如果我重新启动应用程序,插入的数据将显示在控件中(特别是一个组合框,但我将在将来使用更多)

问题是,在我重新启动应用程序之前,新行根本不会显示,我对数据绑定的理解是,控件应该自动更新自己。我已经阅读了有关INotifyPropertyChanged的内容,但我不确定在这里使用它是否正确,如果正确,如何将其用于行插入

我像这样绑定组合框:

DataSet.tagDataTable tagdata = new tagTableAdapter().GetData();
comboBox1.DataSource = tagdata;
comboBox1.DisplayMember = tagdata.tagnameColumn.ColumnName;
comboBox1.ValueMember = tagdata.tagIDColumn.ColumnName;
Guid g = Guid.NewGuid();
new tagTableAdapter().Insert(g, Name)
// retrieve the datatable from the control
DataSet.tagDataTable tagdata = comboBox1.DataSource as DataSet.tagDataTable;
// create a new row and fill in the data
var dr = tagdata.NewRow();
dr["tagid"] = Guid.NewGuid();
dr["tagname"] = Name;
// actually add it to the table
tagdata.Rows.Add(dr);
像这样插入:

DataSet.tagDataTable tagdata = new tagTableAdapter().GetData();
comboBox1.DataSource = tagdata;
comboBox1.DisplayMember = tagdata.tagnameColumn.ColumnName;
comboBox1.ValueMember = tagdata.tagIDColumn.ColumnName;
Guid g = Guid.NewGuid();
new tagTableAdapter().Insert(g, Name)
// retrieve the datatable from the control
DataSet.tagDataTable tagdata = comboBox1.DataSource as DataSet.tagDataTable;
// create a new row and fill in the data
var dr = tagdata.NewRow();
dr["tagid"] = Guid.NewGuid();
dr["tagname"] = Name;
// actually add it to the table
tagdata.Rows.Add(dr);
其中Name只是从文本框中提取的字符串

谢谢


编辑:我应该提到,我使用的DB表叫做tag,有两列,tagID和tagname。tagID是GUID,tagname是varchar。

您应该像第一次一样再次绑定数据并刷新控件。它不仅仅显示数据。是机修工。它不是实时的。

您使用的是
TabledGater。Insert
是将数据直接插入数据库中,并且绕过应用程序中的任何通知。如果您这样做,那么您必须按照iefpw所说的那样,重新加载数据表并重新绑定控件

另一种方法是在数据表中添加一行。您可以尝试以下方法:

DataSet.tagDataTable tagdata = new tagTableAdapter().GetData();
comboBox1.DataSource = tagdata;
comboBox1.DisplayMember = tagdata.tagnameColumn.ColumnName;
comboBox1.ValueMember = tagdata.tagIDColumn.ColumnName;
Guid g = Guid.NewGuid();
new tagTableAdapter().Insert(g, Name)
// retrieve the datatable from the control
DataSet.tagDataTable tagdata = comboBox1.DataSource as DataSet.tagDataTable;
// create a new row and fill in the data
var dr = tagdata.NewRow();
dr["tagid"] = Guid.NewGuid();
dr["tagname"] = Name;
// actually add it to the table
tagdata.Rows.Add(dr);
此时,您已将其添加到DataTable中,combobox将自动更新,但需要注意的是,它尚未写入数据库,因此您需要确保在某个时候保存到数据库中

请查收。我相信通过DataTable添加一行将触发必要的通知。如果不是,请尝试将标记数据包装到