DataGridView没有刷新

时间:2014-01-09 18:50:11

标签: sql vb.net datagridview

我有一个类似于这个的SQL表:

1 | a |东西...

2 | a |东西...

3 | b |东西...

4 | a |东西...

5 | b |东西...

我只想表明:

3 | b |东西...

5 | b |东西...

所以我使用这段代码加载DataGridView:

Private Sub GetData()

    Dim objConn As New SqlConnection(sConnectionString)
    objConn.Open()

    ' Create an instance of a DataAdapter.
    Dim daInstTbl As _
        New SqlDataAdapter("SELECT * FROM Table WHERE Column = 'b'", objConn)

    ' Create an instance of a DataSet, and retrieve data from the Authors table.
    daInstTbl.FillSchema(dsNewInst, SchemaType.Source)
    daInstTbl.Fill(dsNewInst)

    ' Create a new instance of a DataTable
    MyDataTable = dsNewInst.Tables(0)

    daInstTbl.Update(dsNewInst)
End Sub
Private Sub InitializeDataGridView()
    Try
        ' Set up the DataGridView. 
        With Me.DataGridView1

            ' Set up the data source.
            .DataSource = MyDataTable

        End With

    Catch ex As SqlException
        MessageBox.Show(ex.ToString, _
            "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        System.Threading.Thread.CurrentThread.Abort()
    End Try
End Sub

一切都很有效,直到我想要删除 3 并重新编号 4 5 一个变为 3 4 。我有循环处理一切,数据库正在接收正确的数据,除了我的DataGridView只显示重新启动程序时的更新。

这是我的删除代码:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
    Dim objConn As New SqlConnection(sConnectionString)
    objConn.Open()
    Dim daInstDeleteTbl As New SqlDataAdapter("SELECT * FROM Table", objConn)
    Dim dsDeleteInst As New DataSet
    Dim MyDeleteTable As DataTable
    ' Create an instance of a DataSet, and retrieve data from the Authors table.
    daInstDeleteTbl.FillSchema(dsDeleteInst, SchemaType.Source)
    daInstDeleteTbl.Fill(dsDeleteInst)

    ' Create a new instance of a DataTable
    MyDeleteTable = dsDeleteInst.Tables(0)

    'Begin Delete Code

    Dim DeleteID, DeleteIndex As Integer
    Dim MadeChange As Boolean = False
    Integer.TryParse(TextBox1.Text, DeleteID)
    Dim dgvIndexCount As Integer = MyDeleteTable.Rows.Count - 1
    If MyDeleteTable.Rows(dgvIndexCount).Item(0) > DeleteID Then
        Dim counter As Integer = -1
        For Each row As DataRow In MyDeleteTable.Rows
            counter += 1
            If row.Item("Column") = DeleteID Then
                DeleteIndex = counter
            End If
        Next
        MadeChange = True
    End If
    drCurrent = MyDeleteTable.Rows.Find(DeleteID)
    drCurrent.Delete()
    If MadeChange = True Then
        Dim i As Integer = 0
        For i = DeleteIndex + 1 To dgvIndexCount
            MyDeleteTable.Rows(i).Item(0) = MyDeleteTable.Rows(i).Item(0) - 1
        Next
    End If

    'Send Changes to SQL Server
    Dim objCommandBuilder As New SqlCommandBuilder(daInstDeleteTbl)
    daInstDeleteTbl.Update(dsDeleteInst)

    Dim daInstTbl As _
        New SqlDataAdapter("SELECT * FROM Table WHERE Column = 'b'", objConn)
    Dim objCommandReBuilder As New SqlCommandBuilder(daInstTbl)
    daInstTbl.Update(dsNewInst)

End Sub

我认为我做了很多额外的工作只是为了做错了。有任何想法吗?感谢。

2 个答案:

答案 0 :(得分:1)

当您调用SqlDataAdapter.Update()时,适配器会通过执行相应的INSERTUPDATEDELETE (from MSDN)来更新数据库中的值。 SELECT命令未执行。所以你需要这样做:

插入/更新/删除:

daInstTbl.Update(dsNewInst)

选择

daInstTbl.Fill(dsNewInst)

提交:

dsNewInst.AcceptChanges()

示例

Private connection As SqlConnection
Private adapter As SqlDataAdapter
Private data As DataSet
Private builder As SqlCommandBuilder
Private grid As DataGridView

Private Sub InitData()
    Me.SqlSelect(firstLoad:=True, fillLoadOption:=LoadOption.OverwriteChanges, acceptChanges:=True)
    Me.grid.DataSource = Me.data
    Me.grid.DataMember = "Table"
End Sub

Public Sub SaveData()
    Me.SqlInsertUpdateAndDelete()
    Me.SqlSelect(fillLoadOption:=LoadOption.OverwriteChanges, acceptChanges:=True)
End Sub

Public Sub RefreshData(preserveChanges As Boolean)
    Me.SqlSelect(fillLoadOption:=If(preserveChanges, LoadOption.PreserveChanges, LoadOption.OverwriteChanges))
End Sub

Private Sub SqlSelect(Optional firstLoad As Boolean = False, Optional ByVal fillLoadOption As LoadOption = LoadOption.PreserveChanges, Optional acceptChanges As Boolean = False)

    If (firstLoad) Then
        Me.data = New DataSet()
        Me.connection = New SqlConnection("con_str")
        Me.adapter = New SqlDataAdapter("SELECT * FROM Table WHERE Column = 'b'", connection)
        Me.builder = New SqlCommandBuilder(Me.adapter)
    End If

    Me.connection.Open()

    If (firstLoad) Then
        Me.adapter.FillSchema(Me.data, SchemaType.Source, "Table")
    End If

    Me.adapter.FillLoadOption = fillLoadOption
    Me.adapter.Fill(Me.data, "Table")

    If (acceptChanges) Then
        Me.data.Tables("Table").AcceptChanges()
    End If

    Me.connection.Close()

End Sub

Private Sub SqlInsertUpdateAndDelete()

    If (Me.connection.State <> ConnectionState.Open) Then
        Me.connection.Open()
    End If

    Me.adapter.Update(Me.data, "Table")

    Me.connection.Close()

End Sub

PS :(未经测试的代码)

答案 1 :(得分:0)

我的解决方案是改变我声明变量的方式。在我之前:

Dim daInstTbl As _
    New SqlDataAdapter("SELECT * FROM Table WHERE Column = 'b'", objConn)

影响是调用子例程忽略了这一行,因为先前已经声明了变量daInstTbl。解决方案是:

' Delete old instance of a Data____ classes
    da = Nothing
    ds = Nothing
    dt = Nothing

    If GetAll = False Then
        da = New SqlDataAdapter(sSelCmd, objConn)
    Else
        da = New SqlDataAdapter(sSelAllCmd, objConn)
    End If

    ' Create an instance of a DataSet, and retrieve data from the Authors table.
    ds = New DataSet
    da.FillSchema(ds, SchemaType.Source)
    da.Fill(ds)

这清除了信息并允许我分配新值。我的子程序通过分配两个查询字符串然后使用可选的布尔值来确定要使用的版本来实现双重任务。

Dim sSelCmd As String = "SELECT * FROM Table WHERE Coloumn= 'b'"
Dim sSelAllCmd As String = "SELECT * FROM Table"

Private Sub GetData(Optional ByVal GetAll As Boolean = False)
    Dim objConn As New SqlConnection(sConnectionString)
    objConn.Open()

这导致上面的代码!谢谢您的帮助。你的一些概念让我的想法转向正确的方向,并激励我将代码清理成更易读的形式。