Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > add rows to bound datatable
 

add rows to bound datatable

Hi all,
I have a datatable that is bound to datagridview.
How can add to the datatable throught the datagridview.
faris_zaid  Tuesday, January 29, 2008 1:36 AM

You can use the TableAdapter object, specifyInsertCommand and UpdateCommand for it, and call the Update() method toupdate the tables, for more information about this and sample codes, check the following documents:

How to: Bind Data to the Windows Forms DataGridView Control
TableAdapter Overview

Zhi-Xin Ye  Wednesday, January 30, 2008 2:15 PM

And a quick sample for you:

Code Snippet

private void button1_Click(object sender, EventArgs e)

string connstr = "Persist Security Info=False;Integrated Security=SSPI;" +

"Initial Catalog=Northwind;server=localhost";

SqlConnection conn = new SqlConnection(connstr);

string sql = "INSERT INTO TestTable(col1,col2,col3) VALUES(@c1,@c2,@c3)";

SqlCommand cmd = new SqlCommand(sql, conn);

//col1, col2, col3 are columns of the DataTable

"@c1", SqlDbType.VarChar, 255, "col1");

"@c2", SqlDbType.VarChar, 255, "col2");

"@c3", SqlDbType.VarChar, 255, "col3");

SqlDataAdapter da = new SqlDataAdapter();

//dt is a DataTable

You can use the TableAdapter object, specifyInsertCommand and UpdateCommand for it, and call the Update() method toupdate the tables, for more information about this and sample codes, check the following documents:

How to: Bind Data to the Windows Forms DataGridView Control
TableAdapter Overview

Zhi-Xin Ye  Wednesday, January 30, 2008 2:15 PM

And a quick sample for you:

Code Snippet

private void button1_Click(object sender, EventArgs e)

string connstr = "Persist Security Info=False;Integrated Security=SSPI;" +

"Initial Catalog=Northwind;server=localhost";

SqlConnection conn = new SqlConnection(connstr);

string sql = "INSERT INTO TestTable(col1,col2,col3) VALUES(@c1,@c2,@c3)";

SqlCommand cmd = new SqlCommand(sql, conn);

//col1, col2, col3 are columns of the DataTable

"@c1", SqlDbType.VarChar, 255, "col1");

"@c2", SqlDbType.VarChar, 255, "col2");

"@c3", SqlDbType.VarChar, 255, "col3");

SqlDataAdapter da = new SqlDataAdapter();

//dt is a DataTable

Thank you
faris_zaid  Thursday, January 31, 2008 12:03 PM

You can use google to search for other answers

Custom Search

More Threads

• Can't view Access Database in a grid for a User Control
• Winforms datagrid sorting
• Selecting multiple items in list box after retrieving the values from the database
• ListView Item Keys
• updating DataGridView when underlying data has changed
• Bounding the "Enabled" property of a button to whether another object is null and refresh problems
• about aligning one column in datagridview
• Initializing a ComboBoxColumn in DataGridView
• How to populate a listview?
• Mailing Concept