DataGridView WinForms自动重新加载/更新/刷新

时间:2012-02-16 06:52:47

标签: c# vb.net winforms datagridview

我有一个带DataGridView控件的窗体。

我将它绑定到附加的DB文件(.mdf)。

我通过生成动态Insert语句来执行插入。然后我将此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。

有人可以告诉我该怎么做吗? 感谢的

P.S虽然我在VB中这样做,但我不介意在C#中接收答案

4 个答案:

答案 0 :(得分:3)

首先,由于使用SQLCommand命名空间,因此必须使用ParametersSQLClient来避免sql注入。试试这个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

答案 1 :(得分:1)

如果您使用的是ADO.net,请使用.EndEdit() / .Validate()方法

答案 2 :(得分: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

答案 3 :(得分:0)

您可以通过编程方式将dataSource设置为null并再次设置实际dataSource以重新加载网格视图。

但是控件会挂起几秒钟。(甚至suspendLayout可能无效)

除此之外,我不知道如何刷新..即使你刷新,更新等它也不会更新一次绑定的数据网格控件