2012-02-16 59 views
1

我有一個窗口與DataGridView控制。DataGridView WinForms自動重新加載/更新/刷新

我將它綁定到一個附加的DB文件(.mdf)中。

我通過生成動態插入語句來執行插入。然後,我將這個sql語句泵入一個SqlCommand對象並執行ExecuteNonQuery()方法。所有這些都通過處理Button點擊事件來執行。該按鈕和gridview位於相同的窗體上。

Public Sub InsertRow(ByVal param1 As String, ByVal param2 As String, ByVal param3 As String) 

    Dim strConn As String = (the connection string) 
    Dim sqlConn As New SqlConnection(strConn) 

    Dim insertSQL As String = "INSERT INTO theTable VALUES ('" + param1 + "', '" + param2 + "', '" + param3 + "', '" + DateTime.Now + "', '" + DateTime.Now + "')" 
    Dim comm As New SqlCommand(insertSQL, sqlConn) 
    sqlConn.Open() 
    comm.ExecuteNonQuery() 
    sqlConn.Close() 
End Sub 

Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click 

    InsertRow("a","b","c") 

End Sub 

代碼執行後,DataGridView未更新(保持不變)。我必須退出表單並重新加載它以獲得更新後的gridview。我不能使用DataTable對象。 但我希望每次運行插入時更新此gridview。

有人可以告訴我該怎麼做嗎? 感謝的

PS雖然我在VB中這樣做,我不介意在C#

回答

3

首先,你必須使用SQLCommandParameters以避免sql注入,因爲您正在使用SQLClient命名空間。試試這個作爲你的Insert程序。

Private Sub InsertSQL(ByVal param1 As String, ByVal param2 As String, ByVal param3 As String) 

    Using sqlConn As New SqlConnection("ConnectionStringHere") 
     Using sqlComm As New SqlCommand() 
      sqlComm.Connection = sqlConn 
      sqlComm.CommandType = CommandType.Text 
      sqlComm.CommandText = "INSERT INTO theTable VALUES (@Param1,@Param2,@Param3,@Param4,@Param5)" 
      With sqlComm.Parameters 
       .AddWithValue("@Param1", param1) 
       .AddWithValue("@Param2", param2) 
       .AddWithValue("@Param3", param3) 
       .AddWithValue("@Param4", Now) 
       .AddWithValue("@Param5", Now) 
      End With 

      Try 
       sqlConn.Open() 
       sqlComm.ExecuteNonQuery() 
      Catch ex As SqlException 
       MsgBox(ex.Message.ToString, MsgBoxStyle.Exclamation, "Error No. " & ex.ErrorCode.ToString) 
      Finally 
       sqlConn.Close() 
      End Try 

     End Using 
    End Using 

End Sub 

其次,你爲什麼不喜歡用DataTable綁定你的DataGridView?那麼,這是另一個解決方案。它正在使用SQLDataReader,您必須循環才能將記錄放入網格中。

Private Sub ReloadGrid(ByVal connectionString As String) 
    Dim queryString As String = "Your Query Here" 

    Using connection As New SqlConnection(connectionString) 
     Dim command As New SqlCommand(queryString, connection) 
     connection.Open() 

     Dim reader As SqlDataReader = command.ExecuteReader() 

     DataGridView1.Rows.Clear() ' Clear All Rows ' 

     While reader.Read() 
      ' Console.WriteLine(String.Format("{0}, {1}", reader(0), reader(1))) ' 
      ' Insert the record in your datagrid ' 
      Dim row As String() = New String() {reader(0).ToString, reader(1).ToString, reader(2).ToString} 
      DataGridView1.Rows.Add(row) 
     End While 

     ' Call Close when done reading. ' 
     reader.Close() 
    End Using 
End Sub 
+0

這真是太棒了!很好的答案! – rofans91 2012-02-16 07:39:34

+0

@deletedaccount很高興我幫了你! :) – 2012-02-16 07:42:02

+0

@deletedaccount:它似乎你想要一個現成的代碼.. :) – dotNETbeginner 2012-02-16 07:48:31

1

接收答案,如果你正在使用ADO.net使用.EndEdit()/.Validate()方法

+0

我正在使用SqlConnection&SqlCommand,那裏沒有這樣的方法。 – rofans91 2012-02-16 07:06:32

+0

你是否以編程方式執行綁定?你如何綁定它,你在哪裏叫你的函數綁定 – 2012-02-16 07:08:17

+0

不是,而不是編程。我只需使用內置的Visual Studio功能設置所有內容。我只需點擊幾下就可以選擇數據源。 – rofans91 2012-02-16 07:10:24

0

您必須指定一個過程或函數負責重新加載數據,您可以使用它來調用用於將數據加載到datagrid的方法。

Public Sub InsertRow(ByVal param1 As String, ByVal param2 As String, ByVal param3 As String) 

    Dim strConn As String = (the connection string) 
    Dim sqlConn As New SqlConnection(strConn) 

    Dim insertSQL As String = "INSERT INTO theTable VALUES ('" + param1 + "', '" + param2 + "', '" + param3 + "', '" + DateTime.Now + "', '" + DateTime.Now + "')" 
    Dim comm As New SqlCommand(insertSQL, sqlConn) 
    sqlConn.Open() 
    comm.ExecuteNonQuery() 
    sqlConn.Close() 
    ReLoadData(grid) 
End Sub 

Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click 

    InsertRow("a","b","c") 

End Sub 

Private Sub ReloadData(ByVAl sender as DataGridView) 
' Implement your data load function 
' I use for example 
    sender.datasource = GetTable() 'GetTable is a function that return a DataTable Object With my data 
    sender.DataSource = Nothing 'Free the DataGridView DataSource property for enable row edition. 

End Sub 
+0

我不喜歡使用'DataTable'。我其實正在試圖找到另一種方式來做到這一點。我在我的問題中提到過這個問題。 – rofans91 2012-02-16 07:24:24

+0

只要你設置'sender.DataSource = Nothing',gridview就會是空的我想,如果我沒有錯的話.. – dotNETbeginner 2012-02-16 07:25:35

+0

你..爲什麼你將datasource設置成任何沒有權限後,它綁定到數據表? – rofans91 2012-02-16 07:45:47

0

您可以編程設置dataSourcenull,並重新設置實際dataSource重新加載gridview的..

但控制將掛起一段幾秒鐘..(甚至suspendLayout可能無法工作)

除此之外,我不知道如何刷新..即使你刷新,更新等也不會更新綁定一次DataGrid控件

+0

我正在考慮這樣做。但由於某些原因,我無法將數據源中的數據轉換爲任何數據集。 – rofans91 2012-02-16 07:22:43

+0

@deletedaccount:是性能原因還是其他原因? – dotNETbeginner 2012-02-16 07:30:45

+0

這是一個巨大的表格(很多或行,可能還有很多大型列)? 是這個表訪問/更新了很多? 如果是這樣,您可能需要更改GUI以允許用戶選擇子集來顯示/編輯表格 – gg89 2016-05-27 14:21:22