简体   繁体   English

DataGridView数据检索混乱

[英]DataGridView data retrieval confusion

I'm trying to retrieve data from database after clicking view button from windows form, but every time I click on view button the same data in database copied in multiple rows in DataGridView control, instead of retrieving the same data from starting in the DataGridView every time I click on view button. 我试图从Windows窗体单击“查看”按钮后从数据库检索数据,但是每次我单击“查看”按钮时,数据库中的相同数据复制到DataGridView控件的多行中,而不是从每个DataGridView中开始检索相同的数据我单击查看按钮的时间。

     //For view button 
        private void button2_Click(object sender, EventArgs e)
        {
                BindingSource bindingSource = new BindingSource();
                bindingSource.DataSource = businesslayer.View("Select * from itemmaster"); //passed to business access layer class
                dataGridView1.DataSource = bindingSource;

        } 


//Method in DataAccessLayer class
 public DataTable View(String query)
        {
            //Initialize a connection object
            OpenConn();

            //Initalize a command object with passing string value
            command = new SqlCommand(query, connection);
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = command;

            //Fill dataset with a table
            da.Fill(dataset, thisTable);
            return dataset.Tables[thisTable];
        }

Could anyone please tell me how can I solve this problem 谁能告诉我如何解决这个问题

BindingSource bindingSource = new BindingSource(); 
dataGridView1.Items.Clear();
bindingSource.DataSource = businesslayer.View("Select * from itemmaster"); //passed to business access layer class 
dataGridView1.DataSource = bindingSource;

Try clearing your gridview before binding it to your datasource. 将其绑定到数据源之前,请尝试清除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM