无法刷新绑定到 DataTable 的 DataGridView
Posted
技术标签:
【中文标题】无法刷新绑定到 DataTable 的 DataGridView【英文标题】:Unable to refresh DataGridView that is bound to a DataTable 【发布时间】:2019-04-11 22:58:50 【问题描述】:我已经使用论坛上的其他答案尝试了所有方法。我只是希望我的数据网格视图在进行更改后选择表单上的更新按钮时动态更新。
参考下面的代码,当前的结果是当我添加一个新行并按下更新按钮时,数据网格视图只是将所有现有记录(和新行)附加到下面,因此列表的大小会随着重复而不断增长价值观。
public UserGroupsGridViewForm()
InitializeComponent();
private void UserGroupsGridViewForm_Load(object sender, EventArgs e)
LoadUserGroupsToDataTable();
public static SqlCommandBuilder userGroupsSqlCommandBuilder;
public static DataTable userGroupsDataTable = new DataTable();
public static SqlDataAdapter userGroupsSqlAdaptor;
public void LoadUserGroupsToDataTable()
try
SqlConnection connection = new SqlConnection(connectionString);
string cmdText1 = "SELECT * FROM [dbo].[UserGroups]";
userGroupsSqlAdaptor = new SqlDataAdapter(cmdText1, connection);
userGroupsSqlCommandBuilder = new SqlCommandBuilder(userGroupsSqlAdaptor);
userGroupsSqlAdaptor.Fill(userGroupsDataTable);
catch (Exception ex)
log.Error(ex);
SystemEvents.DatabaseExceptions(ex);
LoadDataTabletoGridView();
private void LoadDataTabletoGridView()
try
UserGroupsGridView1.DataSource = userGroupsDataTable;
catch (Exception ex)
SystemEvents.DatabaseExceptions(ex);
private void SaveChangesButton_Click(object sender, EventArgs e)
userGroupsSqlAdaptor.Update(userGroupsDataTable);
//UserGroupsGridView1.Update(); // not working!
//UserGroupsGridView1.Refresh(); // not working!
LoadUserGroupsToDataTable();
【问题讨论】:
【参考方案1】:好的,所以我从 Microsoft 找到了一个相当新的示例,它解决了我的查询。官方指南可以在这里找到:
我对 Microsoft 示例所做的唯一真正更改是在我的表单上结合更新和重新加载(使用单个保存按钮)这两种方法,以便立即反映更改。
private void UserGroupsGridViewForm_Load(object sender, EventArgs e)
LoadDataTabletoGridView();
private readonly BindingSource bindingSource1 = new BindingSource();
private SqlDataAdapter dataAdapter = new SqlDataAdapter();
public void GetData(string selectCommand)
try
SqlConnection connection = new SqlConnection(connectionString);
//string cmdText1 = "SELECT * FROM [dbo].[UserGroups]";
// Create a new data adapter based on the specified query.
dataAdapter = new SqlDataAdapter(selectCommand, connection);
// Create a command builder to generate SQL update, insert, and
// delete commands based on selectCommand.
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
// Populate a new data table and bind it to the BindingSource.
DataTable table = new DataTable
Locale = CultureInfo.InvariantCulture
;
dataAdapter.Fill(table);
bindingSource1.DataSource = table;
catch (Exception ex)
log.Error(ex);
SystemEvents.DatabaseExceptions(ex);
private void LoadDataTabletoGridView()
// Bind the DataGridView to the BindingSource
// and load the data from the database.
UserGroupsGridView.DataSource = bindingSource1;
GetData("SELECT * FROM [dbo].[UserGroups]");
private void SaveChangesButton_Click(object sender, EventArgs e)
// Update the database with changes.
dataAdapter.Update((DataTable)bindingSource1.DataSource);
// Reload the data from the database.
GetData(dataAdapter.SelectCommand.CommandText);
【讨论】:
docs.microsoft.com/en-us/dotnet/framework/winforms/controls/…以上是关于无法刷新绑定到 DataTable 的 DataGridView的主要内容,如果未能解决你的问题,请参考以下文章
怎么把数据库中的数据通过jquery easyui datagrid进行绑定绑定
使用 TemplateColumns 将 WPF DataGrid 绑定到 DataTable
怎么让datagridview不自动修改绑定的datatable
如何在运行时将 Kendo Grid 与 System.Data.DataTable 绑定
无法将属性绑定到“FlywayProperties”,AnnotationConfigApplicationContext@5454d35e 尚未刷新