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. 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
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. 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.
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
- make a default sub fillgrid()
- call it after any update
- it reload your grid
You will need an event for example a button click. To keep this simple in the button event make the datagridview = yourtableAdptor.getdata();
This will keep the view up-to-date as long as the insert statements are before the getdata sample code.
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();
}
精彩评论