Click here to Skip to main content
16,004,587 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello everyone,
i have question if you could help me please :)

i have temporary datagridview im my application i want to add new data to row
but its only updating row so is there any way any advice pleas
this is my code
C#
private void button1_Click(object sender, EventArgs e)
       {
           dataGridView1.AutoGenerateColumns = true;
           DataTable dtb = new DataTable();
           dtb.Columns.Add("Column1");
           dtb.Columns.Add("Column2");
           dtb.Columns.Add("Column3");

           DataRow dtr = dtb.NewRow();

           dtr["Column1"] = textBox1.Text;
           dtr["Column2"] = textBox2.Text;
           dtr["Column3"] = textBox3.Text;

           dtb.Rows.Add(dtr);

           dataGridView1.DataSource = dtb;

           //dataGridView1.DataBind();
       }


thanks for any type of answers ...
Posted
Comments
j snooze 25-Aug-15 17:49pm    
Is this a web app or a winforms? Basically your problem is you are making a new datatable everytime. putting a single row in it and binding it to the gridview. You need to store that datatable somewhere and get rid of the
DataTable dtb = new DataTable();
line. How you store or hold that datatable depends on if this is a web app or winforms.
jame01 26-Aug-15 5:28am    
thanks for you answer..
im using winform
so there is no way i can do that?

add values directly inside grid like this
dataGridView1.Rows.Add(textBox1.Text,textBox2.Text,textBox3.Text);
 
Share this answer
 
thank you guys for your answer,
i got solution and hope be useful for someone in someday ,,,,,
C#
dataGridView1.ColumnCount = 2;
        dataGridView1.ColumnHeadersVisible = true;

        
        dataGridView1.Columns[0].Name = "name";
        dataGridView1.Columns[1].Name = "age";
        
     string[] row1 = new string[] {textbox1.tex,texbox2.tex};
  
object[] rows = new object[] { row1 };

        foreach (string[] rowArray in rows)
        {
            dataGridView1.Rows.Add(rowArray);
        }
    }
 
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