带有MySQL的C#将数据添加到绑定源停止在记录#8

时间:2016-04-15 10:56:41

标签: c# mysql data-binding datagridview

我总是无法加载DataGridView客户。他们总是冻结在记录#8

我的app名称空间下有一个Customers类,如此

public class Customers
    {
        public string No { get; set; }
        public string ID { get; set; }
        public string NoSPU { get; set; }
        public string Name { get; set; }
        public string Telp { get; set; }
        public string Kavling { get; set; }
        public string Tipe { get; set; }
        public string Pokok { get; set; }
        public string Bunga { get; set; }
    }

这是我的代码,在我的DataGridView中将items添加到customersBindingSource并且我放入了formLoad事件

string query = "select * from customer";           
            customersBindingSource.Clear();
            Int32 i = 0;
            MySqlDataReader reader = dx.findQuery(query);
            while (reader.Read())
            {
                i++;
                customersBindingSource.Add(new Customers() {
                    No = i.ToString(),
                    ID = reader.GetString("id"),
                    NoSPU = reader.GetString("nospu"),
                    Name = reader.GetString("nama"),
                    Telp = reader.GetString("telp"),
                    Kavling = reader.GetString("kavling"),
                    Tipe = reader.GetString("tipe") 
                });
                MessageBox.Show(i.ToString()+" OKE");
            }
            reader.Close();

如果我尝试在客户表上使用小于8的数据,它始终有效,但当我添加超过8的新客户时,它总是冻结,客户表单不会显示。我的代码有限制或有问题吗?

1 个答案:

答案 0 :(得分:1)

尝试这样:

// Create and populate the list of DemoCustomer objects
// which will supply data to the DataGridView.
List<DemoCustomer> customerList = new List<DemoCustomer>();
customerList.Add(DemoCustomer.CreateNewCustomer());
customerList.Add(DemoCustomer.CreateNewCustomer());
customerList.Add(DemoCustomer.CreateNewCustomer());

// Bind the list to the BindingSource.
this.customersBindingSource.DataSource = customerList;
希望它可以帮助你。