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.
//For view button
private void button2_Click(object sender, EventArgs e)
{
Bi开发者_JAVA技巧ndingSource 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.
精彩评论