Solved

VB 2008  - Delete all DataGridView records at one time

Posted on 2009-07-08
9
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 6

Expert Comment

by:BALMUKUND KESHAV
ID: 24802675
0
 
LVL 6

Accepted Solution

by:
BALMUKUND KESHAV earned 500 total points
ID: 24802706
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
ID: 24802711
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ljcor
ID: 24802722
OK, that looks pretty much like what I need.  Can you tell me what  BuildDataList()  does?
0
 
LVL 6

Expert Comment

by:BALMUKUND KESHAV
ID: 24810671
@ 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
ID: 24811756
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
ID: 24811854
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
ID: 24812350
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
ID: 24812448
Thanks very much for your help and instruction.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

751 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