Refer below code.
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
BindGrid();
}
private void BindGrid()
{
//Hide the last blank line.
dataGridView1.AllowUserToAddRows = false;
//Clear Columns.
dataGridView1.Columns.Clear();
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("Sr.No", typeof(int)),
new DataColumn("SName", typeof(string)),
new DataColumn("Phone No",typeof(string)),
new DataColumn("Address",typeof(string)) });
dt.Rows.Add(1, "A", "12345", "H");
dt.Rows.Add(2, "B", "54321", "G");
dt.Rows.Add(3, "C", "23456", "C");
dt.Rows.Add(4, "D", "76543", "D");
dt.Rows.Add(5, "E", "34567", "B");
dt.Rows.Add(6, "F", "87654", "A");
dt.Rows.Add(7, "G", "34557", "M");
dt.Rows.Add(8, "H", "56789", "N");
dataGridView1.DataSource = dt;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
}
public static int count = 0;
private void timer1_Tick(object sender, EventArgs e)
{
string message = RowMessage();
if (!string.IsNullOrEmpty(message))
{
Thread.Sleep(5000); // 5 Seconds.
Delay(100).ContinueWith(_ => MessageBox.Show("Name\tNumber\tAddress\n" + message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information));
}
else
{
timer1.Stop();
}
}
private string RowMessage()
{
string message = "";
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
if (count < dataGridView1.Rows.Count)
{
string name = dataGridView1.Rows[count].Cells[1].Value.ToString();
string phone = dataGridView1.Rows[count].Cells[2].Value.ToString();
string address = dataGridView1.Rows[count].Cells[3].Value.ToString();
message += name + "\t" + phone + "\t" + address + "\n";
count++;
if (count % 3 == 0)
{
break;
}
}
else
{
break;
}
}
return message;
}
public static Task Delay(double milliseconds)
{
var tcs = new TaskCompletionSource<bool>();
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += (obj, args) =>
{
tcs.TrySetResult(true);
};
timer.Interval = milliseconds;
timer.AutoReset = false;
timer.Start();
return tcs.Task;
}