【问题标题】:How can I refresh DataGridView?如何刷新 DataGridView?
【发布时间】:2015-06-25 06:37:39
【问题描述】:

我正在尝试在添加多个记录后刷新 datagridview,这些记录是使用 bindingsource 和 datasource 以编程方式添加的。我的代码:

Private Async Function RefreshData() As Task

    Await Task.Delay(15000)

    bs.EndEdit()
    daProducts.Update(dtProducts)

    DataGridView1.DataSource = Nothing
    DataGridView1.DataSource = bs 'Insert your DataSource here
    bs.ResetBindings(False)
    ...

当我在表单加载事件中调用此函数时,它无法显示任何记录。

另一件事是,如果我不使用异步函数(因为设置 DefaultCellStyle.Alignment)会出错。

我的表单加载事件:http://sudrap.org/paste/text/554747/

【问题讨论】:

  • 所以调用 RefreshData 后你的 DataGridView 是空的?
  • 是的,调用函数后DataGridView1为空
  • 但是你在Form_Load中调用它...所以如果你不调用 RefreshData dataGridView1 不为空?
  • 不,如果我不调用该函数,它就不是空的。感谢您的帮助。

标签: vb.net visual-studio-2013 datagridview


【解决方案1】:

这可能对你有帮助:

Private Sub updatedgv()
        Dim conn As New MySqlConnection(My.Settings.myConn)
        Dim da As New MySqlDataAdapter
        Dim ds As New DataSet
        Dim str1 As String = "select * from tableName"
        da.SelectCommand = New MySqlCommand(str1, conn)
        da.Fill(ds)
        conn.Close()
        ProductDataGridView.DataSource = ds.Tables(0)
End Sub

解释可以在链接中找到 http://www.codeproject.com/Questions/372731/how-to-refresh-datagridview-in-vb-net

【讨论】:

  • 它似乎有效。我需要一些时间来尝试一下。谢谢你的回答。
  • @nikel 如果您已经测试过代码,请将其标记为答案。
  • 使用您的代码后,单击标题不会排序。为什么会这样?
  • 我认为如果你添加 yourdatagrdview.Refresh() 它也会起作用。
  • 很抱歉听到这个消息 :( ,不知道你为什么会这样,因为我认为你使用的代码与这个排序问题无关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-02
  • 2013-08-18
  • 1970-01-01
  • 2020-09-23
  • 2015-09-03
相关资源
最近更新 更多