我已经向底层数据库添加了一条记录,添加记录后我执行了 datagridview.Refresh(); 而且我没有看到新添加的记录。
如果我停止并启动应用程序,它就在那里。我在做什么或不做什么?注意:button1 和 datagridview 的形式不同。我公开了 datagridview 的修饰符。这个项目是 ado.net 项目
public class CustomerService
{
public List<Customers> ShowAll()
{
List<Customers> customers = new List<Customers>();
SqlConnection conn = new SqlConnection("data source=.; database=custer; user id=sa; password=*****");
SqlCommand cmd = new SqlCommand(" select * from Customers ", conn.Open());
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Customer customer = new Customer ()
{
CustomerID = dr.GetInt32(0),
CustomerName = dr.GetString(1),
CustomerSurname = dr.GetString(2),
};
customers.Add(customer);
}
conn.Close();
return customers;
}
}
private void button1_Click(object sender, EventArgs e)
{
CustomerService service = new CustomerService();
if (txtCustomerName.Text != "" || txtCustomerSurname.Text != "")
{
customerservice.customerAdd(txtCustomerName.Text, txtCustomerSurname.Text);//this rows is other method .I am using for adding new customer
MessageBox.Show("Customer Added");
Form1.dataGridView1.DataSource = service.ShowAll();
Form1.dataGridView1.Refresh();
}
else
{
//……………
}
}