.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> DataGrid >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

How To Delete Selected Row From The Gridview?

Posted By:Ghanashyam Nayak       Posted Date: September 10, 2010    Points: 10    Category: DataGrid    URL: http://www.dotnetspark.com  

It Will Delete The Selected Row From The GirdView & Database Both At Runtime...
 


This Article Is For GridBiew Control. A GridView Control Which Connected With The Database & If User Want To Remove Selected Row From That GridView & Also Delte that Row From The Database....


For That Table Looks Like :

====================
GridTabel :
====================
GridId | GridName
====================
1 | LB1
2 | LB2
3 | LB3
4 | LB4
5 | LB5
====================




01.private void button1_Click(object sender, EventArgs e)
02.{  
03.  System.Collections.ArrayList alv = new System.Collections.ArrayList();
04.  int i = 0;
05.  
06.  
07.    OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=<>");
08.    OleDbCommand cmd=new OleDbCommand();
09.    for (int i = 0; i < dataGridView1.Rows.Count; i++)
10.    {
11.      DataGridViewRow MyRow = dataGridView1.Rows[i];
12.      if (MyRow.Selected == true)
13.      {
14.        dataGridView1.Rows.RemoveAt(i);
15.        try
16.        {
17.          conn.Open();
18.          //Here It Will Delete your Row From The Database Depending On Your Id....
19.          cmd.CommandText = "DELETE FROM GridTable Where GridId=" + i + "";
20.          cmd.ExecuteNonQuery();
21.          conn.Close();
22.        }
23.        catch(Exception ex)
24.        {
25.          MessageBox.Show(ex.ToString());
26.        }
27.      }
28.    }
29.  }
30.}




     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend