Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want the rows to appear one at a time, having a delay before each row as it appears in datagrid

What I have tried:

I have tried this code but it shows all the data from the database all at once, what i want is that it would show the rows 1 at a time with a delay of lets say 500 millisecond before the next row appear.

private async void button_Load_Click(object sender, EventArgs e)
{
await a.DisplayData(dataGridView1);


var connection = @"connectionstring";
var command = @"SELECT * FROM table";
var data = await LoadData(command, connection);
this.dataGridView1.DataSource = data;
}

public async Task<datatable> LoadData(string connection, string command)
{
var dt = new DataTable();
using (var adapt = new SqlDataAdapter(connection, command))
await Task.Run(() => adapt.Fill(dt));
//await Task.Delay(1000);
return dt;
Posted
Updated 18-Jun-21 5:44am
Comments
Richard MacCutchan 18-Jun-21 11:37am    
You really should have put this in your previous question rather than opening a new one.

1 solution

If you want each row to appear "slowly", as if it was new and just arrived, you can't just move a Fill operation to a separate thread. What you'd have to do is use a DataReader instead of a DataAdapter, read each row, add it to your DGV, and then Sleep the thread for a small period of time - a second or so, maybe - before adding the next. Of course, that presents its own problems, because you can't access the DataGridView at all except from the main (UI) thread so you also have to Invoke the DGV in order to more the "Add a row" code back to the UI thread.

To be honest, I wouldn't use Task or await code for this because it's going to get really messy really quickly.
Instead, I'd use a BackgroundWorker and pass each row to the UI thread as part of the Progress reporting it has built in. Get a row, report progress on it, Sleep the BackgroundWorker, repeat until out of rows. The code would be a lot cleaner, and much easier to modify.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900