Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I created a form in which I added a dataGridView and four textBoxes and Two Buttons (Add,Delete).
My problem is when i select a Row in dataGridView and then Click on Delete button it remove that row from dataGridView but it doesn't effect on database.
My code is :-
C#
//In form load event
DC.SelectSta("select proName as Name,proComp as Company,proQty as Qty,proUnit as Unit from orderList", "orderList");
            dataGridView1.DataSource = DC.dataSet;
            dataGridView1.DataMember = "orderList";

//delete Button click event
try
{
    DialogResult dr = MessageBox.Show("Do You want to delete the product details?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
    if (dr == DialogResult.Yes)
    {
        dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
        DC.adpt.Update(DC.dataSet, "orderList");
    }
}
catch
{ }


Thanks In advance....
Posted

hi
you need to write code to delete from database within if condition.
 
Share this answer
 
Comments
JayantaChatterjee 28-Mar-13 8:31am    
How do I get selected field value from dataGridView??
vinayraghavendra.hs 28-Mar-13 8:39am    
http://www.codeproject.com/Questions/338684/get-value-from-selected-rows-in-datagridview refer this link you will come to know.
JayantaChatterjee 28-Mar-13 9:02am    
thanks a Loooooooooooot ..
It work fine.....
On delete button click try to delete data from database.
Then retrieve data & bind to grid.
 
Share this answer
 
Comments
JayantaChatterjee 28-Mar-13 8:29am    
can you give me more suggestion on that..
like some code or Algorithm...
add dataGridView1.databind();
 
Share this answer
 
Comments
JayantaChatterjee 28-Mar-13 8:28am    
Its not ASP.NET,
I used Windows application...
Try this,

C#
if (dr == DialogResult.Yes)
    {
DC.dataSet.Tables["orderList"].Rows[CurrentRow.Index].Delete();
DC.dataSet.AcceptChanges();
         dataGridView1.DataSource = DC.dataSet;
            dataGridView1.DataMember = "orderList";
dataGridView1.DataBind();
    }
 
Share this answer
 
Comments
JayantaChatterjee 28-Mar-13 8:36am    
Not working ....
same result, Its delete row from dataGridView but not in Database...
and I use Windows Form not WEB application...
sudeshchandram 28-Mar-13 8:49am    
In the code that I have given, delete dataGridView1.DataBind(); and add the below line

DC.adpt.Update(DC.dataSet, "orderList");

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900