我如何刷新DataGridView?

时间:2015-01-19 11:05:19

标签: vb.net winforms datagridview

当我更新其他Form中的某些行时,我正在尝试刷新DataGridView。我正在以这种方式在load事件中加载DataGridView:

Private Sub Home_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try
       (... Nothing important...)

        Me.StockBasicoTableAdapter1.FillByOwner(Me.SctmpruebasDataSet.StockBasico, idCompany)

    Catch exception As Exception
        BarraEstado.Text = exception.Message
    End Try

End Sub

因此,当我更新数据时,我使用REST将其发送到我的Java API并更新数据库中的数据。所以我的问题是我不知道如何更新Home窗体中的DataGridView以查看更新的行。

 Private Sub BtnRegularizar_Click(sender As Object, e As EventArgs) Handles BtnRegularizar.Click, MyBase.Enter
    Try
       (... Here I update all the data ...)

        VariablesGlobales.PutRequest("http://localhost:8084/SCTM/api/UpdateBulto", bulto.ToString)

        Dim _dialogSuccess As OperacionCompletada = New OperacionCompletada()
        _dialogSuccess.Owner = Me
        _dialogSuccess.ShowDialog()

        Dim company = VariablesGlobales.usuario.Item("company")
        Dim idCompany = Integer.Parse(company.Item("idCompany").ToString)

        Home.StockBasicoTableAdapter1.FillByOwner(Home.SctmpruebasDataSet.StockBasico, idCompany)
    Catch exception As Exception
        Me.BarraEstado.Text = exception.Message()
    End Try
End Sub

我以这种方式使用Home.DataGridView1.refresh(),即使是两次,也没有任何结果。

感谢您的回复。

2 个答案:

答案 0 :(得分:0)

refresh方法与它无关,它只重新绘制控件。

有关刷新方法的详细信息,请查看以下链接:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.refresh(v=vs.110).aspx

您需要重置datasource gridview控件。{/ p>

示例:

Home.DataGridView1.DataSource = you_data_object

答案 1 :(得分:0)

在这里看看如何做得更优雅,一步一步

 'Open the mountains file geodatabase feature class
  Dim mountainsTable As Table = Table.OpenFileGeodatabaseTable("C:\Data\Romania.gdb", "mountains")

  'Create a new TableBindingAdapter object for the mountains Table
  Dim tableAdapter As TableBindingAdapter = New TableBindingAdapter(mountainsTable)

  'Set the UseCodedValueDomains property to display the descriptive name for the values in any
  'columns which have a CodedValueDomainDefined
  tableAdapter.UseCodedValueDomains = True

  'Set the UseColumnAliasNames property to display alias names in the Column headers.
  tableAdapter.UseColumnAliasNames = True

  'Fill the adapter with all the rows from the mountains Table
  tableAdapter.Fill()

  'Note that the BindingSource component and the DataGridView control would normally be
  'instantiated by dragging and dropping them onto a Windows Form from the toolbox.


  'Create a new BindingSource component
  Dim bindingSource1 As System.Windows.Forms.BindingSource = New System.Windows.Forms.BindingSource()
  'Set the DataSource to be the tableAdapter object
  bindingSource1.DataSource = tableAdapter

  'Create a DataGridView control
  Dim dataGridView1 As System.Windows.Forms.DataGridView = New System.Windows.Forms.DataGridView()
  'Set the Datasource to be the bindingSource1 object 
   dataGridView1.DataSource = bindingSource1