Hello Code Project members
what i want to do is when i press a button it will add data into datagridview
in that
TextHeaders - TextHeaders - CountdownHeaders
John Martin 09:54:30
the row that ends the countdown in this table will be added to a listbox
please help me with this, the code in most of the topics I found on the internet is missing or wrong
What I have tried:
i tried this code
public Form1()
{
InitializeComponent();
label1.Text = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");
dispatcher.Enabled = true;
}
int i = 0;
private void dispatcher_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;
}
I found this code on the internet