我无法重新启动就无法在Datagridview中看到新元素 [英] I cant see new element in Datagridview without restart

查看:49
本文介绍了我无法重新启动就无法在Datagridview中看到新元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将记录添加到基础数据库中,添加记录之后,我执行了datagridview.Refresh();.我看不到新添加的记录.

如果我停止并在那里启动应用程序.我在做什么或不做什么?
注意:button1和datagridview的格式不同.我公开了datagridview的修饰符.
该项目是Windows应用程序.


I have added a record to the underlying db and after I add the record i do a datagridview.Refresh(); and i dont see the newly added record.

If i stop and start the application its there. What am I doing or not doing?
Notes : button1 and datagridview is in different Forms. I made datagridview''s Modifiers public.
This project is Windows application.


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 != "")
        {
            musteriservice.MusteriEkle(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
        {
            //……………
        }
    }    }

推荐答案

如果要绑定ASP.net中的数据,请使用dataGridView1.databind()方法.
让我们知道您是在asp.net还是Windows应用程序中绑定?
If you are binding the data in ASP.net make use of dataGridView1.databind() method.
Let us know are you binding in asp.net or a windows application?


这篇关于我无法重新启动就无法在Datagridview中看到新元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆