Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# DataGrid WinForm
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 :-
//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 28-Mar-13 3:19am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

hi
 
you need to write code to delete from database within if condition.
  Permalink  
Comments
Jayanta Chatterjee at 28-Mar-13 8:31am
   
How do I get selected field value from dataGridView??
vinay raghavendr at 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.
Jayanta Chatterjee at 28-Mar-13 9:02am
   
thanks a Loooooooooooot ..
It work fine.....
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

On delete button click try to delete data from database.
Then retrieve data & bind to grid.
  Permalink  
Comments
Jayanta Chatterjee at 28-Mar-13 8:29am
   
can you give me more suggestion on that..
like some code or Algorithm...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

add dataGridView1.databind();
  Permalink  
Comments
Jayanta Chatterjee at 28-Mar-13 8:28am
   
Its not ASP.NET,
I used Windows application...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Try this,
 
if (dr == DialogResult.Yes)
    {
DC.dataSet.Tables["orderList"].Rows[CurrentRow.Index].Delete();
DC.dataSet.AcceptChanges();
         dataGridView1.DataSource = DC.dataSet;
            dataGridView1.DataMember = "orderList";
dataGridView1.DataBind();
    }
  Permalink  
Comments
Jayanta Chatterjee at 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 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 28 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100