Results 1 to 7 of 7

Thread: Want to delete the data using the ListView

  1. #1
    Join Date
    Aug 2006
    Posts
    162

    Want to delete the data using the ListView

    I am fairly new to the Visual Basic and I am stuck in a problem. I am not able to delete the data using the ListView. When I tried coding for that it is not working and I am not getting the desired output. I am made code that retrieves and inputs data into a table. Even I can update the data using the form on a ListView. But I am not getting the proper coding for deleting the data from the table using the delete button whilst having the row selected in the ListView. I am using the Visual Studio 2005. Hope that somebody can help me soon.!!
    Technology is a way of organizing the universe so that man doesn't have to experience it.-- Max Frisch 1911 -1991

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Want to delete the data using the ListView

    Here is the code for deleting the data from the ListView. Using this code you can delete the whole row of the data. I have also included message box in the coding so that it will ask for the confirmation. Here is the code for deletion of data :
    Code:
    Sub BtnDeleteClick(sender As Object, e As EventArgs)
        Dim selItem As New ListViewItem
        Dim result As DialogResult
            
        result = MessageBox.Show("Are you sure you want to delete the selected item(s)?", _
                                     "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            
        If result = DialogResult.No Then
            MessageBox.Show("Delete action has been cancelled.", "Delete Cancelled", _
                MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            For Each selItem In listViewRec.SelectedItems
                selItem.Remove()
            Next
        End If
    End Sub
    If you select No after the confirmation asked, then the data will not get deleted. Clicking on Yes will delete the data. Hope that this code help you.

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: Want to delete the data using the ListView

    If you want to display and modify the data in the ListView control, you can follow the steps given below :
    1. Right-click the project, click Add ASP.NET Folder, and then click App_Data, if the Web site does not have an App_Data folder, in Solution Explorer.
    2. Then right-click the App_Data folder in the Solution Explorer, and then click Add Existing Item. After doing this the Add Existing Item dialog box will be displayed.
    3. Then enter the location of the AdventureWorks database file (AdventureWorks_Data.mdf) was installed. The path "C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\AdventureWorks_Data.mdf" is the default path where the .mdf file is installed.
    4. Then Open or Switch to the Default.aspx file.
    5. Later Switch to Design view.
    6. From the Data tab of the Toolbox, drag a ListView control onto the page.
    7. In the Common ListView Tasks menu, in the Choose Data Source drop-down list, click <New data sourceā€¦>.

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Want to delete the data using the ListView

    I think that this code will work :
    Code:
    Private Sub Command3_Click()
    'If con.State = adStateOpen Then
        Dim con As ADODB.Connection
        Set con = New ADODB.Connection
        con.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=\\asfserver\itp$\Product_tabletest.mdb")
        Set rs = New ADODB.Recordset
        Dim x As Integer
        If Not con Is Nothing Then 'added check to see if con is set to a valid object
            For x = ListView1.ListItems.Count To 1 Step -1
                If ListView1.ListItems(x).Checked = True Then
                   con.Execute "DELETE from Supplier where Supplier_Name='" & ListView1.ListItems(x).SubItems(1) & "'"
                   ListView1.ListItems.Remove x
                   End If
            Next
            con.Close
            Set con = Nothing
        Else
            MsgBox "Connection not set"
        End If
       End Sub
    If your problem is not solved you can post the error.

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Want to delete the data using the ListView

    If you are using the SQL as your database, then after following the steps mentioned by the 'opaper' you will have to follow these steps :
    • Where you can specify the data that you want to retrieve from the database, the Configure Data Source wizard is displayed.
    • Select the Specify a custom SQL statement or stored procedure option. Then follow the instructions given on the screen.
    • Enter the SQL query to retrieve department data from the Works database in the Define Custom Statements or Stored Procedures page.
    • Then click on the tab to do the according things. Like Click the INSERT tab and then enter the following SQL query to insert department data in the Works database.
    • Click the UPDATE tab and then enter the following SQL query to update department data in the Works database.
    • Click the DELETE tab and then enter the following SQL query to delete department data from the Works database.
    • Click Test Query to make sure that you are retrieving the data that you want.
    • Lastly click on the Finish.
    The wizard creates a SqlDataSource control and adds it to the page. The ListView control that you added earlier is bound to the SqlDataSource control.

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Want to delete the data using the ListView

    According to me the code mentioned by the 'absolute55' should work. I have not tried it for myself, but looking at the code I can guess that it should work. The only thing is that, if you are using the SQL and you try to delete the row using the coding then you can only delete the data from the ListView, but that data will remain in the SQLTable. If you want to delete the data from the SQLTable also then you will have to make changes in that coding.

  7. #7
    softtrickseo Guest

    Re: Want to delete the data using the ListView

    Just Put commandname of your Linkbutton
    and write below code in ItemCommand event of ListView

    protected void ListViewJobs_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
    if (e.CommandName.Equals("DeleteJob"))
    {
    ListViewDataItem currentItem = (ListViewDataItem)e.Item;
    Guid jobid = new Guid(ListViewJobs.DataKeys[currentItem.DisplayIndex].Value.ToString());
    JobController obj = new JobController();

    new JobController().CancelJob(jobid);


    }
    }

Similar Threads

  1. how to use Vscrollbars with Listview
    By Suffix in forum Software Development
    Replies: 1
    Last Post: 01-10-2011, 11:46 AM
  2. Transfer the data to another listview
    By Chhaya in forum Software Development
    Replies: 3
    Last Post: 05-10-2009, 12:19 PM
  3. ListView or ListBox
    By Heather5 in forum Software Development
    Replies: 3
    Last Post: 29-08-2009, 12:10 AM
  4. limits on listview
    By Christopher in forum Software Development
    Replies: 3
    Last Post: 17-10-2008, 03:07 PM
  5. Will reinstalling WIN XP delete my data ?
    By Ihit in forum Windows XP Support
    Replies: 4
    Last Post: 25-01-2005, 04:14 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,711,732.25166 seconds with 17 queries