Click here to Skip to main content
15,888,301 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
C#
        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
Posted
Updated 11-Mar-22 23:19pm
Comments
Graeme_Grant 25-Feb-22 8:04am    
Looks okay to me... What happens when you run the code that you found?
Stephanie Shy 25-Feb-22 8:06am    
text in datagridview keeps changing
Richard MacCutchan 25-Feb-22 10:50am    
That is because of your timer. You need to clarify what you are trying to do with this.
Stephanie Shy 25-Feb-22 8:06am    
This is sentence1
This is sentence2
This is sentence3

Loop
Graeme_Grant 25-Feb-22 8:10am    
Then the sample works. Now you need to adapt to your needs.

1 solution

Looks like you want to keep the data table for more than a tick, so move it and its header construction out of the tick event to prevent it from being destroyed and recreated each tick. And yes^, you have all you need. The rest is on you to figure it out. that's programming.
 
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