1

m developing application for my office and in my application there is a datagrid view that linked to a mysql database. local users can update the database using datagridview but they cant delete any recodes. i want to implement a method that user's are select exact raw in the datagridview and delete it as well as delete the database record soon. i managed to make select and delete datagridview row using below code but it not update the database

private void button60_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewCell oneCell in dataGridView1.SelectedCells)
            {
                if (oneCell.Selected)
                    dataGridView1.Rows.RemoveAt(oneCell.RowIndex);
            }

        }

and this is my connection string that i normally use to view the database data in datagrid view. i don't have good knowledge to combined these two.can someone please show me how to do that

my connection string

private void showdatagrid() {

        string constring = string.Format("datasource='{0}';username=******;port=3306;password=***********;Connect Timeout=20000;Command Timeout=28800", dbserverip.Text);
        MySqlConnection conwaqDatabase = new MySqlConnection(constring);
        MySqlCommand cmdwaqDatabase = new MySqlCommand(" select * from warit.loans ; ", conwaqDatabase);


        try
        {

            MySqlDataAdapter sda = new MySqlDataAdapter();
            sda.SelectCommand = cmdwaqDatabase;
            dbdataset = new DataTable();
            sda.Fill(dbdataset);
            BindingSource bsource = new BindingSource();

            bsource.DataSource = dbdataset;
            dataGridView1.DataSource = bsource;
            sda.Update(dbdataset);

        }


        catch (Exception ex)
        {


            MessageBox.Show(ex.Message);

        }
9
  • where you're removing row from data gridview also use 'delete from table_name where column_name=value' Sep 6, 2014 at 16:37
  • thank you for the answer can you please edit this code for me because i already tried more than 10 times but i cant figure it out. i'm new to programming :( Sep 6, 2014 at 16:40
  • do you have a primary key in your database table? Sep 6, 2014 at 16:43
  • No i assigned a primary key as a auto-increment value, i want to delete exact datadridvew row what user selected Sep 6, 2014 at 16:45
  • what is the criteria to delete the row? which column value you will use to delete the row? e.g FirstName, SSN etc Sep 6, 2014 at 16:46

2 Answers 2

0

try this code

private void button60_Click(object sender, EventArgs e)
{
    foreach (DataGridViewCell oneCell in dataGridView1.SelectedCells)
    {
        if (oneCell.Selected)
        {
            dataGridView1.Rows.RemoveAt(oneCell.RowIndex);
            int loannumber = dataGridView1.Rows[oneCell.RowIndex].Cells['index of loannumber column in datagridview'].Value; // assuming loannmber is integer
            string username = dataGridView1.Rows[oneCell.RowIndex].Cells['index of username column in datagridview'].Value; // assuming username is string
            /* Now create an object of MySqlConnection and MySqlCommand
             * and the execute following query
             */
            string query = string.Format("DELETE FROM table_name WHERE loannumber = {0} AND username = '{1}'", loannumber, username);
        }
    }

}
8
  • Thanks sir but what are the values should i replace with index values ? i put 0 and 7 but it's not working Sep 6, 2014 at 17:43
  • do you replace the 'table_name' and what is the data types of 'loannumber' and 'username'? Sep 6, 2014 at 17:44
  • yes data values are correct int and string and i replaced the table_name names :( Sep 6, 2014 at 17:49
  • i tried to post my whole code here but it's too long sir what can i do ? Sep 6, 2014 at 17:53
  • can you use teamviewer? Sep 6, 2014 at 17:54
0
string connection = "server = URL; uid = user; pwd = password; database = database;";

using (MySqlConnection conn = new MySqlConnection(connection)) {

    conn.Open();

    string idLocRemv = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();

    string removeVolCred = "DELETE FROM TableName WHERE ID = " + idLocRemv;

    using (MySqlCommand command = new MySqlCommand(removeVolCred, fbcConn)) {
        command.ExecuteNonQuery();
    }

    conn.Close();
}

Apply necessary exception handlers. (try catch finally)

Additionally, you will need to re-load DataGridView afterwards.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.