Retrieving a DataTable from a Collection by name
-
Is there a direct way to access a datatable in a collection directly by its name? Currently, the only way I know how access a datatable is by using a foreach statement to find the table I wish to update/modify:
DataTableCollection tables = collection.Tables; foreach (DataTable tbl in tables) { string name = tbl.TableName; if (name == "name") { //Update datatable with new row DataRow myNewRow; myNewRow = tbl.NewRow(); code..... tbl.Rows.Add(myNewRow); } }
Thanks for any help -
Is there a direct way to access a datatable in a collection directly by its name? Currently, the only way I know how access a datatable is by using a foreach statement to find the table I wish to update/modify:
DataTableCollection tables = collection.Tables; foreach (DataTable tbl in tables) { string name = tbl.TableName; if (name == "name") { //Update datatable with new row DataRow myNewRow; myNewRow = tbl.NewRow(); code..... tbl.Rows.Add(myNewRow); } }
Thanks for any helpUmm... perhaps DataTableCollection.Item[^]
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
Umm... perhaps DataTableCollection.Item[^]
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
Thanks for the answer. I've not used datatable collections before and was spinning my wheels. So I can replace the foreach loop with: DataTable table = tables["name"];
Yep. Should work for all collections!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
Is there a direct way to access a datatable in a collection directly by its name? Currently, the only way I know how access a datatable is by using a foreach statement to find the table I wish to update/modify:
DataTableCollection tables = collection.Tables; foreach (DataTable tbl in tables) { string name = tbl.TableName; if (name == "name") { //Update datatable with new row DataRow myNewRow; myNewRow = tbl.NewRow(); code..... tbl.Rows.Add(myNewRow); } }
Thanks for any help:) ds = DB.FillDataSet(cmd, "StRecord"); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); foreach (DataColumn col in ds.Tables[0].Columns ) { } From: http://www.programlive.tk