简体   繁体   English

如何在 vb.net 中刷新 Datagridview

[英]How to refresh Datagridview in vb.net

In my VB.net win form application, when I clicked Load button I am displaying a filename from a folder onto a Datagridview.在我的 VB.net win 表单应用程序中,当我单击加载按钮时,我将文件夹中的文件名显示到 Datagridview 上。 Then after I click on Process button the file will be moved to another folder.然后在我点击处理按钮后,文件将被移动到另一个文件夹。 After file has been moved, Grid has to be refreshed.移动文件后,必须刷新网格。

Here is the code i have written.这是我写的代码。 I can able to move the file but not refreshing the Grid.Any suggestions please?我可以移动文件但不能刷新网格。请问有什么建议吗?

 Public Class Form1
 Private Sub Load_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Load.Click

    With DataGridView1
        .Columns.Add("Column 0", "TaskName")
        .AutoResizeColumns()
    End With

    Dim rowint As Integer = 0

    'Dim directoryInfo As New System.IO.DirectoryInfo("C:\Users\Desktop\auto")
    'Dim fileInfo = System.IO.Directory.GetFiles(directoryInfo.ToString)
    'Dim name As String

    DataGridView1.Rows.Add()
    Dim filename As String = System.IO.Path.GetFileName("C:\Users\Ram\Desktop\auto\INQUEUE\123.txt")
    DataGridView1.Item(0, rowint).Value = filename
    rowint = rowint + 1

End Sub

Private Sub Process_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Process.Click
    System.IO.File.Move("C:\Users\Ram\Desktop\auto\INQUEUE\123.txt", "C:\Users\Ram\Desktop\Demo\abc.txt")
    System.IO.File.Delete("C:\Users\Ram\Desktop\auto\INQUEUE\123.txt")
    DataGridView1.Refresh()
End Sub

End Class结束 Class

Because you are not binding to anything, it would be best to just change the value of the row in the grid.因为您没有绑定到任何东西,所以最好只更改网格中行的值。

The better alternative would be to create a List and then set your datasource to that, and when you update the list item to reflect the new string, you can then refresh the grid and it should work.更好的选择是创建一个列表,然后将您的数据源设置为该列表,当您更新列表项以反映新字符串时,您可以刷新网格并且它应该可以工作。

Private Sub Process_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Process.Click    
    System.IO.File.Move("C:\Users\Ram\Desktop\auto\INQUEUE\123.txt", "C:\Users\Ram\Desktop\Demo\abc.txt")    
    System.IO.File.Delete("C:\Users\Ram\Desktop\auto\INQUEUE\123.txt")    
    DataGridView1.Rows(0).Cells(0) = "C:\Users\Ram\Desktop\auto\INQUEUE\123.txt"    
End Sub

The Refresh() method only redraws the existing grid to the screen again. Refresh()方法仅将现有网格重新绘制到屏幕上。 You will need to reload the grid's data by performing a "click".您将需要通过执行“单击”来重新加载网格的数据。 This can be done by calling the event directly or by using the PerformClick() method.这可以通过直接调用事件或使用PerformClick()方法来完成。

Private Sub Process_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Process.Click
    System.IO.File.Move("C:\Users\Ram\Desktop\auto\INQUEUE\123.txt", "C:\Users\Ram\Desktop\Demo\abc.txt")
    System.IO.File.Delete("C:\Users\Ram\Desktop\auto\INQUEUE\123.txt")
    Load_Click(Load, Nothing)
    DataGridView1.Refresh()
End Sub

---- or ---- - - 或者 - -

Private Sub Process_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Process.Click
    System.IO.File.Move("C:\Users\Ram\Desktop\auto\INQUEUE\123.txt", "C:\Users\Ram\Desktop\Demo\abc.txt")
    System.IO.File.Delete("C:\Users\Ram\Desktop\auto\INQUEUE\123.txt")
    Load.PerformClick()
    DataGridView1.Refresh()
End Sub

You will need an event for example a button click.您将需要一个事件,例如单击按钮。 To keep this simple in the button event make the datagridview = yourtableAdptor.getdata();为了在按钮事件中保持简单,使datagridview = yourtableAdptor.getdata(); This will keep the view up-to-date as long as the insert statements are before the getdata sample code.只要插入语句在 getdata 示例代码之前,这将使视图保持最新。

For example:例如:

private void button1_Click(object sender, EventArgs e)
{
    decimal pay = Convert.ToDecimal(textBox1.Text);
    string comment = textBox2.Text;

    payTableAdapter.Insert(dateTimePicker1.Value, pay, comment);

    payDataGridView.DataSource = payTableAdapter.GetData();

    textBox1.Clear();
    textBox2.Clear();
}
  • make a default sub fillgrid()制作一个默认的子填充网格()
  • call it after any update在任何更新后调用它
  • it reload your grid它重新加载你的网格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM