[英]refreshing data grid view doesn't work
I want to refresh data grid view but it doesn't work i have a Refresh method like this :我想刷新数据网格视图,但它不起作用我有这样的 Refresh 方法:
public void Select()
{
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
string cs = "server=(local);database=DB_Taxi;trusted_connection=yes;";
con.ConnectionString = cs;
con.Open();
cmd.Connection = con;
da.SelectCommand = cmd;
cmd.CommandText = "SELECT * FROM Tbl_Driver";
da.Fill(dt);
con.Close();
grid.DataSource = dt;
}
and it is on main form.它在主要形式上。 I want call this function in another form named Add_Driver.我想以另一种名为 Add_Driver 的形式调用此函数。 for that I tell it like this in submit button because I get same text box values and after submit them i want to show them in data grid view from database.为此,我在提交按钮中这样说,因为我得到相同的文本框值,提交后我想在数据库的数据网格视图中显示它们。 I call it like this:我这样称呼它:
private void btnOK_Click(object sender, EventArgs e)
{
if (txtID.Text != "" || txtName.Text != "" || txtLastName.Text != "" || txtMobile.Text != "" ||
txtPhone.Text != "" || txtCar.Text != "" || txtGender.Text != "" || txtAddress.Text != "")
{
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
string cs = "server=(local);database=DB_Taxi;trusted_connection=yes;";
con.ConnectionString = cs;
con.Open();
cmd.Connection = con;
cmd.CommandText = "INSERT INTO Tbl_Driver(DriverID,DName,DLastName,DMobile,DAddress,DCar,DGender,DPhone) VALUES(@ID,@Name,@LastName,@Mobile,@Address,@Car,@Gender,@Phone)";
cmd.Parameters.AddWithValue("@ID", txtID.Text);
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
cmd.Parameters.AddWithValue("@Mobile", txtMobile.Text);
cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
cmd.Parameters.AddWithValue("@Car", txtCar.Text);
cmd.Parameters.AddWithValue("@Gender", txtGender.Text);
cmd.Parameters.AddWithValue("@Phone", txtPhone.Text);
cmd.ExecuteNonQuery();
con.Close();
Empty();
////////////////////
Main m = new Main();
m.Select();
////////////////////
MessageBox.Show("Added");
}
else
{
MessageBox.Show("Plese complete the form");
}
}
but data on data grid view doesn't change.但数据网格视图上的数据不会改变。 please help!请帮忙! but when i call this method on the main form it works but i write like this for main method:但是当我在主窗体上调用此方法时,它可以工作,但我为 main 方法编写如下:
Select()
Try This.尝试这个。
public void Select() {
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
string cs = "server=(local);database=DB_Taxi;trusted_connection=yes;";
con.ConnectionString = cs;
con.Open();
cmd.Connection = con;
da.SelectCommand = cmd;
cmd.CommandText = "SELECT * FROM Tbl_Driver";
da.Fill(dt);
con.Close();
//clearing the datasource
grid.DataSource = null;
//set the datasource
grid.DataSource = dt;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.