如何刷新 dataGridView

发布于 2024-12-29 20:15:10 字数 2943 浏览 3 评论 0原文

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

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

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 函数”!

请帮助我!

I have a problem refreshing a DataGridView control after Insert or Update. The source code:

Get all rows from table in datatable and set to the datasource:

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

Update Event if ID is valued, and Insert if it isn't:

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

I tried to get all records from the table and set the datasource again at the end of the method but it doesn't work.

If I put the code:

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

on end of event method RowLeave I recive this error:

"Operation is not valid because it results in a reentrant call to the
SetCurrentCellAddressCore function"

on dataGrid.Rows.Clear(), but if I remove the line codes Rows.Clear() and Columns.Clear(), the debug cursor after execute dataGrid.DataSource = dt1 return to begin of event method an execute some code again and after I recive the some error "...reentrant call to the SetCurrentCellAddressCore function"!

Help me, please!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光①夏 2025-01-05 20:15:10

这是我用来连接搜索我的 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();
    }

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

Here is a C# class I have that I use to connect search my GridView. What you need should be similar I would think.

    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();
    }

I hope this gets you on the right track.

喜爱纠缠 2025-01-05 20:15:10

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

For solving this problem I use OdbcDataAdapter. I save all changes with adapter.Update(dataTable) and after I fill the datatable again: adapter.fill(dataTable).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文