Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 25th, 2008, 06:16 AM
edurazee
Guest
 
Posts: n/a
Default Inserting data into DataGrid

Can anyone show me how to insert new row data into DataGrid cells and persist the changes in the DB?

The condition is, the total code must be coded by hand.

The reason is, I have written a DB manipulation class. And, the DataGrid data insertion code must work with that.

I have found a similar project on codeproject.com
Code:
Using the DataGrid Control
By Huseyin Altindag
but the author used Visual Studio drag-drop feature to add a DataSet control. So the code came with different functionality.

 
Old July 28th, 2008, 05:51 PM
Authorized User
 
Join Date: Dec 2004
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to DZukiewicz
Default

Well, DataGrids are fairly flexible, so do what you need to with DataTables and then use that to insert into a database. I'll do a small demo from the .cs perspective, not the in-line ASPX code:

Code:
DataTable dt = new DataTable("MyTable");
dt.Columns.Add("Id",typeof(int));
dt.Columns.Add("Name",typeof(string));

dt.Rows.Add( new object[] { 1,"Joe" } );
dt.Rows.Add( new object[] { 2,"Bob" } );
dt.Rows.Add( new object[] { 3,"Jim" } );
dt.Rows.Add( new object[] { 4,"Mick" } );
dt.Rows.Add( new object[] { 5,"Dave" } );

ViewState["DataTable"] = dt;

DataGrid dg = new DataGrid();
dg.AutoGenerateColumns = true;
dg.DataSource = dt;
dg.DataBind();
Binding to a database could be similar to...

Code:
DataTable dt = ViewState["DataTable"] as DataTable;

SqlConnection sqlConn = new SqlConnection("Data Source=localhost;Initial Catalog=mydb;Integrated Security=SSPI");
SqlCommand sqlComm = new SqlCommand();
sqlComm.Connection = sqlConn;
sqlComm.CommandText = "INSERT INTO MyTable (Id,Name) VALUES @Id, @Name";
sqlComm.CommandType = System.Data.Common.CommandType.Text;
sqlCommn.Parameters.Add("@Id", SqlDbType.Int, 4, "Id");
sqlCommn.Parameters.Add("@Name", SqlDbType.VarChar, 10, "Name");

using(SqlDataAdapter da = new SqlDataAdapter(sqlComm))
{
     da.Fill(dt);
}
Does that help at all?

Dominic





Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting multiple rows from datagrid to database ashish0512 ASP.NET 1.x and 2.0 Application Design 2 August 25th, 2012 02:09 AM
inserting data stevens ADO.NET 0 May 13th, 2008 07:49 AM
Data inserting twice nuttylife2 ASP.NET 2.0 Professional 8 August 22nd, 2006 11:01 AM
Inserting data when data already exsist desireemm SQL Language 0 January 16th, 2006 11:01 PM
Inserting text from datagrid into Memo field in c# kgriffin Classic ASP Databases 0 May 4th, 2005 10:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.