Hey guys,


I need a code that would delete the row from not only from
grid view but also from the table.

I am able to delete the row from grid view but its not getting deleted
from table.

Any suggestions on how to do it.


Regards
Ajinya

Recommended Answers

All 4 Replies

Hi ajinya,
Well, you can store the datakey of the selected row and then delete the row from the table too using that datakey.

hey munnaz,

thanks for your reply.

Let me paste my code :

private void button1_Click(object sender, EventArgs e)
        {
           
            OleDbConnection con = new OleDbConnection(str);
            
            OleDbCommand cmd = new OleDbCommand();
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                DataGridViewRow dr = dataGridView1.Rows[i];
                if (dr.Selected == true)
                {
                    dataGridView1.Rows.RemoveAt(i);
                    try
                    {
                        con.Open();
                        cmd.CommandText = "Delete from company where ID=" + i + "";
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }

I tried deleting row through this code but nothing happened.
Can you help me with the code?


Thanks and Regards
Ajinkya

I think you should recall your grid there. Other wise u cannot know whether your record is deleted in code. Because you are just getting the id of the selected row and making it delete from database but not in ur records. So make a try to recall the grid after ur function has completed..

You miss one line of code from your code just place this line between line 17 and 18

da.Update(ds,"company");

Here da is for data adapter, ds for dataset

Hope it will help you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.