Solved

VB 2008  - Delete all DataGridView records at one time

Posted on 2009-07-08
9
222 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

860 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