DataGridView WinForms Auto Reload/Update/Refresh

后端 未结 4 1595
半阙折子戏
半阙折子戏 2021-01-27 02:27

I have a windows form with a DataGridView control.

I bound it into an attached DB file (.mdf).

I perform insertion by generating a dynamic Insert st

4条回答
  •  天命终不由人
    2021-01-27 02:53

    You must specify a procedure or function to be responsible for reloading data, you can use to make a new call to the methods that you use to load data to 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
    

提交回复
热议问题