1

I have a database with MySQL which is connected with C# windows application.
My problem is that I have a form with 2 buttons and datagridview. The one button is exit and the other is clear.
With the button clear I want to clear all the data in the database and I can’t do this.
The only thing I have done is to clear the entries and then when I reopen the entities are there!

THE CODE IS:

private void button3_Click(object sender, EventArgs e)
{ //clear balance
    projectpizzaDataSet ds = new projectpizzaDataSet();
    projectpizzaDataSetTableAdapters.balanceTableAdapter daCust = new projectpizzaDataSetTableAdapters.balanceTableAdapter();

    for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
        dataGridView1.Rows.RemoveAt(i); //delete error 
    }
}
4
  • 1
    With this code you are deleting rows from the DataGridView not from database. Commented May 19, 2012 at 17:52
  • 1
    It seems your question should be, "How to remove records from a database?" Other than refreshing the grid after deletion, the real problem has nothing to do with that control. Commented May 19, 2012 at 18:39
  • Can you post the code for how you are binding your data to the DataGridView? Commented May 19, 2012 at 18:40
  • string y = comboBox2.Text; int x = Convert.ToInt32(textBox1.Text); double p = Convert.ToDouble(textBox6.Text); daCust.Insert(x, y, p); Commented May 20, 2012 at 16:05

3 Answers 3

1

Thank you all but the solution is:

private void button3_Click(object sender, EventArgs e) {
    //clear balance
    projectpizzaDataSet ds = new projectpizzaDataSet();
    projectpizzaDataSetTableAdapters.balanceTableAdapter daCust = new projectpizzaDataSetTableAdapters.balanceTableAdapter();

    while (dataGridView1.Rows.Count > 0 {
        dataGridView1.Rows.RemoveAt(0);
    }

    this.balanceTableAdapter.Update(projectpizzaDataSet.balance);
}
Sign up to request clarification or add additional context in comments.

Comments

0

To delete records from the database, you need to use the DELETE query. here you can find how to use the DELETE command.

Comments

0

You have to Delete the rows from the DataSet and then to Update the TableAdapter

private void button3_Click(object sender, EventArgs e)
{     
    foreach(DataRow row in ds.Tables["TableName"].Rows)
    {
        row.Delete();
    }

    daCust.Update(ds);
}

2 Comments

i try the code but is not working. on the "tablename" i wrote "balance"
Check the links I posted and also this link. I'm sure you will find your answer. Maybe your tableAdatper has a delete method itself. Also post the code on how do you create your dataset

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.