Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

VB 2008 - Delete all DataGridView records at one time

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
ljcor
Asked:
ljcor
  • 5
  • 4
1 Solution
 
BALMUKUND KESHAVCommented:
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
 
ljcorAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ljcorAuthor Commented:
OK, that looks pretty much like what I need.  Can you tell me what  BuildDataList()  does?
0
 
BALMUKUND KESHAVCommented:
@ 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
 
ljcorAuthor Commented:
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
 
ljcorAuthor Commented:
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
 
BALMUKUND KESHAVCommented:
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
 
ljcorAuthor Commented:
Thanks very much for your help and instruction.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now