[英]Refresh datagridview after insert on the same form
I have a form with a datagridview and input boxes with a button to insert values. 我有一个带有datagridview的表单和一个带有用于插入值的按钮的输入框。 I would like the datagridview to refresh once the button is clicked.
单击按钮后,我希望datagridview刷新。 I have tried the datagridview.refresh() and datagridview.update options.
我已经尝试过datagridview.refresh()和datagridview.update选项。 The data only appears once the application has been closed and reopened.
仅在关闭并重新打开应用程序后,数据才会显示。
Here is my code: 这是我的代码:
class cFunction
{
public static void DoSQL(string Query)
{
SqlConnection Connection = new SqlConnection(@" Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Zahida\Desktop\RapidsoftSupport\RapidsoftSupport\MainData.mdf;Integrated Security=True");
SqlCommand Command = new SqlCommand(Query, Connection);
Command.Connection.Open();
Command.ExecuteNonQuery();
Command.Connection.Close();
Connection.Close();
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
cFunction.DoSQL("INSERT INTO Problem(SYSTEM_ID,SUBJECT, KEYWORDS) VALUES('" + SID + "','" + txtSubject.Text + "','" + txtKeywords.Text + "')");
this.problemTableAdapter.Fill(this.mainDataDataSet1.Problem);
}
Any help? 有什么帮助吗?
The grid control must bind to active and alive data source, you push some records to dataset when your program load and grid cache theme to show. 网格控件必须绑定到活动和活动的数据源,在显示程序加载和网格缓存主题时,您将一些记录推送到数据集。
You can use binding or run select query after every insert occurred to update dataset with new records . 您可以在每次插入后使用绑定或运行select查询,以使用新记录更新数据集。
Zahida Kazi, Zahida Kazi,
At first, you don't need to push two closure: 首先,您不需要强制两个关闭:
Command.Connection.Close();
Command.Connection.Close();
Connection.Close(); 的Connection.close();
I didn't see a gridview bindings. 我没有看到gridview绑定。 If you are not set a bindings just now, I can suggest you to use
DataTable
to create Data and bind by DataGridView.DataSource = DataTable
. 如果您现在还没有设置绑定,我建议您使用
DataTable
创建数据并通过DataGridView.DataSource = DataTable
绑定。 When data updated, you can use two ways to update gridview's data 数据更新后,可以使用两种方法来更新gridview的数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.