C# 从子表单更新数据库后刷新datagridview win表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10888561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refresh datagridview win forms after updating the database from a child form
提问by sevoug
how to refresh datagridview after making changes on the database from another form, after closing child form i tried to refresh the datagridview with click event but it's not working, do i have to use dataset ?
如何在从另一个表单更改数据库后刷新 datagridview,关闭子表单后,我尝试使用单击事件刷新 datagridview,但它不起作用,我必须使用数据集吗?
//create an OleDbDataAdapter to execute the query
dAdapter = new OleDbDataAdapter(gQuery, connString);
//create a command builder
cBuilder = new OleDbCommandBuilder(dAdapter);
//create a DataTable to hold the query results
dTable = new DataTable();
//fill the DataTable
dAdapter.Fill(dTable);
//BindingSource to sync DataTable and DataGridView
bSource = new BindingSource();
//set the BindingSource DataSource
bSource.DataSource = dTable;
//set the DataGridView DataSource
dataGridView1.DataSource = bSource;
private void button_Refresh_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = bSource;
dataGridView1.Refresh();
}
Help me, please thanks in advance
帮助我,请提前致谢
回答by Fonzy
have you tried
你有没有尝试过
dataGridView1.DataSource = dTable;
回答by Nitin Gupta
Add
添加
dataGridView1.Update();
It will solve your problem.
它会解决你的问题。
回答by Innocent Uwimana
bSource.DataSource = dTable;
dataGridView1.DataSource = bSource;
it would be better if you will recall your table
如果你能回忆起你的桌子会更好
回答by slowmax
When you link your database with "DataSource" in DataGridView properties, IDE automatically adds BindingSource and TableAdapter to your form.
当您将数据库与 DataGridView 属性中的“DataSource”链接时,IDE 会自动将 BindingSource 和 TableAdapter 添加到您的表单中。
If database is updated and you want to refresh DataGridView, call this:
如果数据库已更新并且您想刷新 DataGridView,请调用:
this.<table name>TableAdapter.Fill(this.<DB name>DataSet.<table name>);
Where <table name>is name of your table (for example Users) and <DB name>is name of your database (for example MyDB).
其中<table name>是您的表名(例如用户)和<DB name>您的数据库名称(例如 MyDB)。
this.UsersTableAdapter.Fill(this.MyDBDataSet.Users);

