Solved

VB 2008  - Delete all DataGridView records at one time

Posted on 2009-07-08
9
218 Views
Last Modified: 2012-08-13
I am not sure how to delete all of the records that appear in the DataGridView at one time.  I do want them to be deleted from the table and the database.
0
Comment
Question by:ljcor
  • 5
  • 4
9 Comments
 
LVL 6

Expert Comment

by:BALMUKUND KESHAV
Comment Utility
0
 
LVL 6

Accepted Solution

by:
BALMUKUND KESHAV earned 500 total points
Comment Utility
The following code works for me in asp.net vb.net :

  Sub Delete_Grid(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        dgEmps.EditItemIndex = e.Item.ItemIndex
      '  Dim TheID As String
      '  TheID = e.Item.Cells(2).Text
        Dim DBConn As SqlConnection
        Dim DBdelete As New SqlCommand
        DBConn = New SqlConnection(strConnection)
        DBdelete.CommandText = "delete from Employee "
       '     & "Where ID = " & TheID
        DBdelete.Connection = DBConn
        DBdelete.Connection.Open()
        DBdelete.ExecuteNonQuery()
        DBConn.Close()
        dgEmps.EditItemIndex = -1
        BuildDataList()
    End Sub

Bm Keshav
0
 

Author Comment

by:ljcor
Comment Utility
I should have been more specific (for sure!):  I want to delete all of the data from a SqlServer DB.  I want to do this when a user clicks a button.
0
 

Author Comment

by:ljcor
Comment Utility
OK, that looks pretty much like what I need.  Can you tell me what  BuildDataList()  does?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 6

Expert Comment

by:BALMUKUND KESHAV
Comment Utility
@ ljcor:
 BuildDataList() is another call which populates datagrid like as under :

Sub BuildDataList ()
        Dim DBConn As MySqlConnection
        Dim DBCommand As MySqlDataAdapter
        Dim DSPageData as New DataSet
        DBConn = New SqlConnection(strConnection)
        DBCommand = New SqlDataAdapter ("Select * From Employee " & "Order By LastName,_ firstName",  DBConn)
        DBCommand.Fill(DSPageData, "Employee")
        dgEmps.DataSource = DSPageData.Tables("Employee").DefaultView
        dgEmps.DataBind()
End Sub

Thanks for the points.

Bm Keshav


0
 

Author Comment

by:ljcor
Comment Utility
Thanks for that additional info.  It does cause me to ask one more question: Does that function fetch ALL fields in the table or ONLY those displayed in the grid?  I assume the latter but I didn't know such a function even existed.
0
 

Author Comment

by:ljcor
Comment Utility
Or is this a function you created and not a system function?  I ask because I cannot find any reference to anything by that name in the reference sources.
0
 
LVL 6

Expert Comment

by:BALMUKUND KESHAV
Comment Utility
Builddatalist is not a system function, its a you can say a function/class or procedure designed by me in our asp.net page to retrieve all data from employee table. You can see sql used in following line
DBCommand = New SqlDataAdapter ("Select * From Employee " & "Order By LastName,_ firstName",  DBConn)
is extracting all fields and all data from employee table. You can put sql a/c to your req. i mean your desired field/data aswell.
You can change like this :
DBCommand = New SqlDataAdapter ("Select field1,field2 From Employee where your creatary", DBConn)

Bm Keshav
0
 

Author Comment

by:ljcor
Comment Utility
Thanks very much for your help and instruction.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Zoom web page in asp.net 2 34
Performance enhancement 39 25
reports, calling from code. 3 29
Format column on datatable 7 14
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now