C# DataGridView数据检索混乱
我试图在从windows窗体单击“查看”按钮后从数据库检索数据,但每次单击“查看”按钮时,数据库中的相同数据都会复制到DataGridView控件的多行中,而不是每次单击“查看”按钮时从DataGridView中开始检索相同的数据C# DataGridView数据检索混乱,c#,winforms,C#,Winforms,我试图在从windows窗体单击“查看”按钮后从数据库检索数据,但每次单击“查看”按钮时,数据库中的相同数据都会复制到DataGridView控件的多行中,而不是每次单击“查看”按钮时从DataGridView中开始检索相同的数据 //For view button private void button2_Click(object sender, EventArgs e) { BindingSource binding
//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];
}
谁能告诉我怎么解决这个问题
BindingSource bindingSource = new BindingSource();
dataGridView1.Items.Clear();
bindingSource.DataSource = businesslayer.View("Select * from itemmaster"); //passed to business access layer class
dataGridView1.DataSource = bindingSource;
在将gridview绑定到数据源之前,请尝试清除它。但是,我在DataGridView类中没有找到任何Items属性,那么它怎么可能是真的呢?它应该是dataGridView1.Items.Clear。