Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Retrieving a DataTable from a Collection by name

Retrieving a DataTable from a Collection by name

Scheduled Pinned Locked Moved C#
helpquestionannouncement
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    boreland
    wrote on last edited by
    #1

    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

    OriginalGriffO M 2 Replies Last reply
    0
    • B boreland

      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

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Umm... perhaps DataTableCollection.Item[^]

      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      B 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Umm... perhaps DataTableCollection.Item[^]

        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

        B Offline
        B Offline
        boreland
        wrote on last edited by
        #3

        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"];

        OriginalGriffO 1 Reply Last reply
        0
        • B boreland

          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"];

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          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.

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • B boreland

            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

            M Offline
            M Offline
            MasttsaM
            wrote on last edited by
            #5

            :) 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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups