datagridview中的数据未更新 [英] data in datagridview is not updating
本文介绍了datagridview中的数据未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
先生,我已经用c#编写了代码:
sir i''ve written the code in c#:
string constr = "Data Source=TUSHAR-PC;Initial Catalog=callcenter;Persist Security Info=True;User ID=sa;Password=password@123";
SqlConnection con = new SqlConnection(constr);
try
{
con.Open();
string sql = "insert into tasks values('" + txtT_id.Text + "','" + txtname.Text + "','" + prir_txtbx.Text + "','" + stat_txtbx.Text + "','" + txtcomments.Text + "','" + dtpdue_date.Text + "','" + dtpcompletiondate + "')";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
dataGridView1.DataSource = tasksBindingSource;
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
数据存储在我创建的sql server数据库中。数据被插入到数据库中,但datagridview中的数据没有更新。
the data is stored in the sql server database that i have created . data is inserted to the database but the data in the datagridview is not updating.
推荐答案
试试这个,
Try this,
dataGridView1.DataSource = tasksBindingSource;
dataGridView1.DataBind();
嗨。保存操作完成后,您必须调用Window_Loaded(发件人,e);
我想你的GridView在一个窗口中。
好运
Hi. after your Save operation finished ,you must Call Window_Loaded(sender,e);
I suppose your GridView in a Window.
Good Luck
尝试下面的代码。
Hi,
try below code.
con.Open();
string sql = "insert into tasks values('" + txtT_id.Text + "','" + txtname.Text + "','" + prir_txtbx.Text + "','" + stat_txtbx.Text + "','" + txtcomments.Text + "','" + dtpdue_date.Text + "','" + dtpcompletiondate + "');";
//to get the new rows.
sql += "SELECT * FROM tasks"; // replace * with column names of the table
SqlCommand cmd = new SqlCommand(sql, con);
cmd.CommandType = CommandType.Text;
DataSet _objDataSet = new DataSet();
SqlDataAdapter _objSqlDataAdapter = new SqlDataAdapter();
cmd.CommandText = sql;
_objSqlDataAdapter.SelectCommand = _objSQLCommand;
_objSqlDataAdapter.Fill(_objDataSet);
dataGridView1.DataSource = _objDataSet.Table[0];
con.Close();
希望有帮助
hope it helps
这篇关于datagridview中的数据未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文