.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 >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How To Delete Selected Row From The Listview?

Posted By:Ghanashyam Nayak       Posted Date: September 08, 2010    Points: 25    Category: C#    URL: http://www.dotnetspark.com  

It Will Delete The Selected Row From The Listview & Also From The Database...
 

//Here I Use The Table As Below :


ListViewTable :

======================
ListId ListName
======================
1 L1
2 L2
3 L3
4 L4
5 L5
6 L6
7 L7
8 L8
9 L9
10 L10
======================



// Code Will Be :


01.private void button1_Click(object sender, EventArgs e)
02.
03.    System.Collections.ArrayList alv = new System.Collections.ArrayList();
04.    int i = 0;
05.    foreach (ListViewItem lv in listView1.SelectedItems)
06.    {
07.            lv.Remove();
08.            //Here It Will Store The Name Of The Listview Items....
09.            alv[i] = lv.Text.ToString();
10.            i++;
11.    }
12.    OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=<>");
13.    OleDbCommand cmd=new OleDbCommand();
14.    try
15.    {
16.            for (i = 0; i < alv.Count; i++)
17.            {
18.                conn.Open();
19.                    //Depending On The Listview Name It Will Also Check That Arraylist Names In The Database & If It Is There Then Delete That Row...
20.                cmd.CommandText = "DELETE FROM ListViewTable Where ListName='" + alv[i].ToString() + "'";
21.                cmd.ExecuteNonQuery();
22.                conn.Close();
23.            }
24.    }
25.    catch(Exception ex)
26.    {
27.            MessageBox.Show(ex.ToString());
28.    }
29.}


//Images Are Also Attached....

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

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