简体   繁体   中英

Periodically adding one row to DataGridView, but all rows get added at the same time

So I've got this code:

Ping ping = new Ping();
for(int i = 0; i<= number_of_pings; i++)
{
    PingReply pingReply = ping.Send(address_to_send);
    dataGridView1.Rows.Add(address_to_send, pingReply.Address, pingReply.RoundtripTime, pingReply.Status);         
}

For some reason when its being pumped into a dataGridView1 , the dataGridview1 has five (or however number of pings I specify) fields of data pumped to it at once. What I'm trying to do is have the dataGridView1 fill up periodically. Eg after each ping has completed, its output to the dataGridView1 and the next ping starts and so on.

Ideas?

您可以在每次添加行后调用 datagridview 的更新方法:

dataGridView1.Update();

One of the best solution for your needs is BackgroundWorker. Define an empty DataTable in your class and put Ping codes into BackgroundWorker_DoWork method. then Add result to DataTable. When this method completed, BackgroundWorker_WorkerComplete has fired. You should run DoWork method again for next ping.

Ping ping = new Ping();

for(int i = 0; i<= number_of_pings; i++)
{
    PingReply pingReply = ping.Send(address_to_send);

    dataGridView1.Rows.Add(address_to_send, pingReply.Address, pingReply.RoundtripTime, pingReply.Status);
    dataGridView1.PerformLayout();
    Application.DoEvents();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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