Hi smile,
Use Timer control to refresh the DataGridView.
Check this example. Now please take its reference and correct your code.
Code
C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
label1.Text = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");
timer1.Enabled = true;
}
int i = 0;
private void timer1_Tick(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("ID"),
new DataColumn("Name"),
new DataColumn("Message"),
new DataColumn("CharactorCount")
});
dt.Rows.Add(1, "ABC", "This is sentence1.This is sentence2.This is sentence3", 60);
dt.Rows.Add(2, "SKY", "This is sentence1.This is sentence2.This is sentence3", 60);
dt.Rows.Add(3, "XYZ", "This is sentence1.This is sentence2.This is sentence3", 60);
DataSet ds = new DataSet();
int count = dt.Rows[0]["Message"].ToString().Split('.').Length;
for (int i = 0; i < count; i++)
{
DataTable dtCopy = dt.Copy();
for (int j = 0; j < dtCopy.Rows.Count; j++)
{
dtCopy.Rows[j]["Message"] = dtCopy.Rows[j]["Message"].ToString().Split('.')[i] + ".";
dtCopy.Rows[j]["CharactorCount"] = Convert.ToInt32(dtCopy.Rows[j]["CharactorCount"]) / count;
}
ds.Tables.Add(dtCopy);
}
BindGrid(ds.Tables[i]);
i++;
if (i == count)
{
i = 0;
}
label1.Text = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");
}
private void BindGrid(DataTable dt)
{
dataGridView1.DataSource = null;
dataGridView1.AutoGenerateColumns = false;
dataGridView1.Columns.Clear();
dataGridView1.ColumnCount = dt.Columns.Count;
dataGridView1.Columns[0].Name = "ID";
dataGridView1.Columns[0].HeaderText = "ID";
dataGridView1.Columns[0].DataPropertyName = "ID";
dataGridView1.Columns[1].HeaderText = "Name";
dataGridView1.Columns[1].Name = "Name";
dataGridView1.Columns[1].DataPropertyName = "Name";
dataGridView1.Columns[2].Name = "Message";
dataGridView1.Columns[2].HeaderText = "Message";
dataGridView1.Columns[2].DataPropertyName = "Message";
dataGridView1.Columns[3].Name = "CharactorCount";
dataGridView1.Columns[3].HeaderText = "Character Count";
dataGridView1.Columns[3].DataPropertyName = "CharactorCount";
dataGridView1.DataSource = dt;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
}
VB.Net
Public Class Form1
Public Sub New()
InitializeComponent()
label1.Text = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss")
timer1.Enabled = True
End Sub
Private i As Integer = 0
Private Sub timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles timer1.Tick
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("ID"),
New DataColumn("Name"),
New DataColumn("Message"),
New DataColumn("CharactorCount")})
dt.Rows.Add(1, "ABC", "This is sentence1.This is sentence2.This is sentence3", 60)
dt.Rows.Add(2, "SKY", "This is sentence1.This is sentence2.This is sentence3", 60)
dt.Rows.Add(3, "XYZ", "This is sentence1.This is sentence2.This is sentence3", 60)
Dim ds As DataSet = New DataSet()
Dim count As Integer = dt.Rows(0)("Message").ToString().Split("."c).Length
For i As Integer = 0 To count - 1
Dim dtCopy As DataTable = dt.Copy()
For j As Integer = 0 To dtCopy.Rows.Count - 1
dtCopy.Rows(j)("Message") = dtCopy.Rows(j)("Message").ToString().Split("."c)(i) & "."
dtCopy.Rows(j)("CharactorCount") = Convert.ToInt32(dtCopy.Rows(j)("CharactorCount")) / count
Next
ds.Tables.Add(dtCopy)
Next
BindGrid(ds.Tables(i))
i += 1
If i = count Then
i = 0
End If
label1.Text = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss")
End Sub
Private Sub BindGrid(ByVal dt As DataTable)
dataGridView1.DataSource = Nothing
dataGridView1.AutoGenerateColumns = False
dataGridView1.Columns.Clear()
dataGridView1.ColumnCount = dt.Columns.Count
dataGridView1.Columns(0).Name = "ID"
dataGridView1.Columns(0).HeaderText = "ID"
dataGridView1.Columns(0).DataPropertyName = "ID"
dataGridView1.Columns(1).HeaderText = "Name"
dataGridView1.Columns(1).Name = "Name"
dataGridView1.Columns(1).DataPropertyName = "Name"
dataGridView1.Columns(2).Name = "Message"
dataGridView1.Columns(2).HeaderText = "Message"
dataGridView1.Columns(2).DataPropertyName = "Message"
dataGridView1.Columns(3).Name = "CharactorCount"
dataGridView1.Columns(3).HeaderText = "Character Count"
dataGridView1.Columns(3).DataPropertyName = "CharactorCount"
dataGridView1.DataSource = dt
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
End Sub
End Class
Screenshot