我正在尝试从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];
}
有谁能告诉我如何解决这个问题
答案 0 :(得分:0)
BindingSource bindingSource = new BindingSource();
dataGridView1.Items.Clear();
bindingSource.DataSource = businesslayer.View("Select * from itemmaster"); //passed to business access layer class
dataGridView1.DataSource = bindingSource;
尝试清除gridview,然后再将其绑定到数据源。