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. Changing Caption in dt

Changing Caption in dt

Scheduled Pinned Locked Moved C#
regexhelp
2 Posts 2 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.
  • M Offline
    M Offline
    MAW30
    wrote on last edited by
    #1

    I have hundred of DataTables and many of the caption names within do not match the column name. Is there a way I can change all captions to match column name.he follows code I tried.

    foreach (DataTable dt in dataSet.Tables) { foreach (DataColumn dc in dt.Columns) { if (dc.DataType == typeof(string)) { dc.Caption = dc.ColumnName.ToString(); } } }

    ' However this is not permanent and I don't see results. Is there a way to make this permanent. I appreciate any help and thanks in advance. Michael

    OriginalGriffO 1 Reply Last reply
    0
    • M MAW30

      I have hundred of DataTables and many of the caption names within do not match the column name. Is there a way I can change all captions to match column name.he follows code I tried.

      foreach (DataTable dt in dataSet.Tables) { foreach (DataColumn dc in dt.Columns) { if (dc.DataType == typeof(string)) { dc.Caption = dc.ColumnName.ToString(); } } }

      ' However this is not permanent and I don't see results. Is there a way to make this permanent. I appreciate any help and thanks in advance. Michael

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

      The Caption property isn't used to set the headers of a DataGridView (for example) when you use it as a DataSource - it probably should have been, but it definitely wasn't! If you want a "friendly name" for your displayed columns, you need to manually set them on the display control via the "HeaderText" property:

              myDataTable = new DataTable();
              myDataTable.Columns.Add("C1");
              myDataTable.Columns.Add("C2");
              myDataTable.Columns\[0\].Caption = "A new caption";
              myDataGridView.Columns.Clear();
              myDataGridView.DataSource = myDataTable;
              for (int i = 0; i < myDataTable.Columns.Count; i++)
                  {
                  myDataGridView.Columns\[i\].HeaderText = myDataTable.Columns\[i\].Caption;
                  }
      

      "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 AntiTwitter: @DalekDave is now a follower!

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