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