Refreshing DataGridView after Update or Delete
-
29 Şubat 2012 Çarşamba 23:15
Hi all, I am trying to figure out how to make it so that my DGV automatically refreshes when I click my Update or Delete buttons in my program. I looked at other threads and couldn't figure out what to do specific to my program. Here is my code:
Public Class Form1
Dim MyConn As DbConn
Private Sub btnQuery_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuery.Click
Try
Dim SQL As String = txtSQL.Text
Dim DT As DataTable = MyConn.GetDataTable(SQL)
DataGridView1.DataSource = DT
Catch ex As Exception
MsgBox("Invalid Query: " & ex.Message)
End Try
End Sub
Private Sub load_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles load.Click
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim DatabaseName As String = OpenFileDialog1.FileName
MyConn = New DbConn(DatabaseName)
End If
txtSQL.Text = "SELECT * FROM Items"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAttributes.Click
Dim r As DataGridViewRow = DataGridView1.SelectedRows(0)
lblRestID.Text = r.Cells("RestaurantID").Value.ToString
lblCatID.Text = r.Cells("CategoryID").Value.ToString
lblItemID.Text = r.Cells("ItemID").Value.ToString
If CDbl(lblRestID.Text) = 1 Then
lblRestName.Text = "McDonald's"
ElseIf CDbl(lblRestID.Text) = 2 Then
lblRestName.Text = "Burger King"
ElseIf CDbl(lblRestID.Text) = 3 Then
lblRestName.Text = "Wendy's"
End If
If CDbl(lblCatID.Text) = 1 Then
lblCatName.Text = "Entree"
ElseIf CDbl(lblCatID.Text) = 2 Then
lblCatName.Text = "Side Order"
ElseIf CDbl(lblCatID.Text) = 3 Then
lblCatName.Text = "Salad"
ElseIf CDbl(lblCatID.Text) = 4 Then
lblCatName.Text = "Dessert"
ElseIf CDbl(lblCatID.Text) = 5 Then
lblCatName.Text = "Breakfast"
ElseIf CDbl(lblCatID.Text) = 6 Then
lblCatName.Text = "Beverage"
End If
txtItem.Text = r.Cells("ItemName").Value.ToString
nudPrice.Value = CDec(r.Cells("Price").Value.ToString)
nudStart.Value = CDec(r.Cells("StartHour").Value.ToString)
nudEnd.Value = CDec(r.Cells("EndHour").Value.ToString)
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim SQL As String = "UPDATE Items SET ItemName='" & txtItem.Text & "', "
SQL &= "Price=" & nudPrice.Value & ", "
SQL &= "StartHour=" & nudStart.Value & ", "
SQL &= "EndHour=" & nudEnd.Value & " "
SQL &= "WHERE ItemID="
SQL &= lblItemID.Text
txtSQL.Text = SQL
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim SQL As String = "DELETE FROM Items WHERE ItemID="
SQL &= lblItemID.Text
txtSQL.Text = SQL
End Sub
End ClassRight now, it closes my DGV and I have to click Load File again and then Run Query to load the file into the DGV...I just want to have it auto refresh...what code would I write and where would I put it?
Thanks!
Tüm Yanıtlar
-
02 Mart 2012 Cuma 11:08Moderatör
Hi,
You can write code in the event handles of save and delete to reload data from database. As this method is inefficient, it is not recommend. You can modify your code to use a DataAdapter to update the database, then the DataGridView can be updated automatically with reload data from database.
Insert/Update/Delete record with DataGridView... Plz Help!!!!
As this forum focus on Windows Workflow Foundation, it is recommend you post a question at Windows Forms forum. Thanks.
Leo Tang [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Yanıt Olarak İşaretleyen LeoTangModerator 07 Mart 2012 Çarşamba 08:24