hi,
i work on a widows application sql server ce database and i want to know how to add the rows of a datagridview to a datatable.
thanks

Recommended Answers

All 6 Replies

>how to add the rows of a datagridview to a datatable./b]

1. Populate/create datatable

DataTable dt=new DataTable();
 dt.Columns.Add("Col1");
 dt.Columns.Add("Col2");
 dt.Rows.Add("A","B");

2. DataBindings

dataGridView1.Datasource=dt;

thanks for the reply but i mean that i have a datagridview and i want to "copy" its rows in a datatable created in my database. i hope you can help me.

>i want to "copy" its rows in a datatable created in my database.

To update dataGridView (Datasource), use dataAdapter or command objects.

can you please explain to me how to do it i tried many codes but it doesn't work.
thnks

>explain to me how to do it i tried many codes

Create an instance of DataAdapter and configure all four commands (insert,select,delete, and update).

Code for MS-SQL server

SqlConnection cn=new SqlConnection("put_here_connection_string");
SqlDataAdapter adp=new SqlDataAdapter("select * from  TableName",cn);
SqlCommandBuilder cmb=new SqlCommandBuilder(adp);

DataTable dt=new DataTable();
//Populates the datatable
adp.Fill(dt);

//DataBindings

dataGridView1.DataSource=dt;

To update/write back changes to database

adp.Update(dt);

thanks a lot it's ok

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.