如何刷新dataGridView

时间:2012-01-31 18:28:53

标签: vb.net datagridview odbc datasource ibm-midrange

我在InsertUpdate之后刷新DataGridView控件时遇到问题。源代码:

从datatable中的表中获取所有行并设置为数据源:

Dim dt1 as DataTable = GetData("SELECT * FROM CLAIMSTATE ")
dataGrid.DataSource = dt1

如果ID是值,则更新事件,如果不是,则更新:

Private Sub dataGrid_RowLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dataGrid.RowLeave

Dim row As DataGridViewRow = CType(sender, DataGridView).Rows(e.RowIndex)
  Dim query As New StringBuilder("")
  If row.Cells(0).Value & "" = "" Then
    query.Append("INSERT INTO CLAIMSTATE ")
    query.Append("(CST_CODE, CST_LABEL, CST_POINTS)")
    query.Append("VALUES ")
    query.Append("(?, ?, ?)")
  Else
    query.Append("Update CLAIMSTATE ")
    query.Append("SET CST_CODE = ?, ")
    query.Append("CST_LABEL = ?, ")
    query.Append("CST_POINTS = ? ")
    query.Append("WHERE CST_ID = ? ")
  End If
  Dim command As New OdbcCommand(query.ToString(), con)
  command.Parameters.Add("@cst_code", OdbcType.Char).Value = row.Cells(1).Value
  command.Parameters.Add("@cst_label", OdbcType.NVarChar).Value = row.Cells(2).Value
  command.Parameters.Add("@cst_points", OdbcType.Decimal).Value = row.Cells(3).Value
  command.Parameters.Add("@cst_id", OdbcType.BigInt).Value = row.Cells(0).Value

  Dim res As Integer = ExecuteNonQuery(command)
End Sub

Public Function GetData(ByRef sqlQuery As String) As DataTable
    Dim command As New OdbcCommand(sqlQuery, con)
    Try
      If con.State = ConnectionState.Closed Then
        con.ConnectionString = conString
        con.Open()
      End If
      Using dr As OdbcDataReader = command.ExecuteReader()
        Dim dt As New DataTable()
        dt.Load(dr)
        Return dt
      End Using
      'con.Close()
    Catch ex As Exception
      MsgBox(ex.Message)
      con.Close()
      Return Null
    End Try
  End Function

Public Function ExecuteNonQuery(ByRef command As OdbcCommand) As Integer
Dim result As Integer = 0
If con.State = ConnectionState.Closed Then
  con.ConnectionString = conString
  con.Open()
End If
'Dim command As New OdbcCommand(sqlQuery, conn)
Try
  'command.Connection = con
  'Dim cmd As New OdbcCommand( sqlQuery, conn)
  result = command.ExecuteNonQuery()
Catch
  result = 0
  If con IsNot Nothing Then
    con.Close()
    command.Dispose()
  End If
Finally
  command.Dispose()
End Try
Return result
End Function

我试图从表中获取所有记录,并在方法结束时再次设置数据源,但它不起作用。

如果我把代码放在:

dataGrid.Rows.Clear()
dataGrid.Columns.Clear()
dt1 = GetData("SELECT * FROM CLAIMSTATE ")
dataGrid.DataSource = dt1

在事件结束方法RowLeave上我发现了这个错误:

  

“操作无效,因为它会导致重新调用   SetCurrentCellAddressCore函数“

在dataGrid.Rows.Clear()上,但是如果我删除行代码Rows.Clear()和Columns.Clear(),执行dataGrid.DataSource = dt1之后的调试游标返回到事件方法的开始执行一些代码再次和我重新发现一些错误“...重新调用SetCurrentCellAddressCore函数”!

请帮帮我!

2 个答案:

答案 0 :(得分:0)

这是我用来连接搜索我的GridView的C#类。你需要的应该与我想的相似。

    protected void lbSearch_Click(object sender, EventArgs e)
    {
        if (txtSearch.Text.Trim().Length > 0)
        {
            odsInbox.FilterExpression =
                string.Format("(l_name LIKE '*{0}*') OR (f_name LIKE '*{0}*') OR (title LIKE '*{0}*')",
                txtSearch.Text);
        }
        else
        {
            odsInbox.FilterExpression = string.Empty;
        }

        gvInbox.DataBind();
    }

    protected void lbClear_Click(object sender, EventArgs e)
    {
        odsInbox.FilterExpression = string.Empty;
        txtSearch.Text = "";
        gvInbox.DataBind();
    }

我希望这能让你走上正轨。

答案 1 :(得分:0)

为了解决这个问题,我使用OdbcDataAdapter。我使用adapter.Update(dataTable)保存所有更改,然后再次填充数据表:adapter.fill(dataTable)。