DataGridView WinForms Auto Reload/Update/Refresh

后端 未结 4 1460
半阙折子戏
半阙折子戏 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:31

    if you are using ADO.net use .EndEdit() / .Validate() method

    0 讨论(0)
  • 2021-01-27 02:36

    First of all, you have to use SQLCommand and Parameters to avoid sql injection since you are using SQLClient namespace. Try this as your Insert procedure.

    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
    

    Secondly, why don't you prefer to use DataTable to bind your DataGridView? Well, here's another solution. It's ny using SQLDataReader and you have to loop on it to put the records in your grid.

    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 讨论(0)
  • 2021-01-27 02:41

    you can programmatically set the dataSource to null and again set the actual dataSource to reload the gridview..

    but the control will hang for some few secs..(even suspendLayout may not work)

    other than this I don't know how to refresh.. even if you refresh, update etc it will not update the once bound datagrid control

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题