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. Web Development
  3. ASP.NET
  4. DataTable Column Naming

DataTable Column Naming

Scheduled Pinned Locked Moved ASP.NET
comquestion
10 Posts 4 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
    munklefish
    wrote on last edited by
    #1

    Hi, I have a populated DataTable of vaying columns and rows. I need to somehow take the data of the 1st row and make this the column name of the column in which it is found eg; Column1 | Column2 | Column3 name | date | email mike | 26 June | me@you.com dave | 05 Apr | him@wibble.co.uk should read as: name | date | email mike | 26 June | me@you.com dave | 05 Apr | him@wibble.co.uk Any suggestions? Many Thanks. :^)

    B V H 3 Replies Last reply
    0
    • M munklefish

      Hi, I have a populated DataTable of vaying columns and rows. I need to somehow take the data of the 1st row and make this the column name of the column in which it is found eg; Column1 | Column2 | Column3 name | date | email mike | 26 June | me@you.com dave | 05 Apr | him@wibble.co.uk should read as: name | date | email mike | 26 June | me@you.com dave | 05 Apr | him@wibble.co.uk Any suggestions? Many Thanks. :^)

      B Offline
      B Offline
      Baran M
      wrote on last edited by
      #2

      In GridView1_RowDataBound event you can do like that, if (e.Row.RowIndex == 0) { e.Row.Cells[0].Text = "Name"; e.Row.Cells[1].Text = "date"; e.Row.Cells[2].Text = "Email"; }

      Education is not a way to escape poverty — it is a way of fighting it.

      M 1 Reply Last reply
      0
      • B Baran M

        In GridView1_RowDataBound event you can do like that, if (e.Row.RowIndex == 0) { e.Row.Cells[0].Text = "Name"; e.Row.Cells[1].Text = "date"; e.Row.Cells[2].Text = "Email"; }

        Education is not a way to escape poverty — it is a way of fighting it.

        M Offline
        M Offline
        munklefish
        wrote on last edited by
        #3

        That doesnt solve the problem unfortunately. As explained i need to take the content of the cell in row1 and make that the column name. This is because i dont know the content of row1 nor do i know how many columns there are, since the info is from an uploaded source. Likewise i dont have a gridview, i have a datatable as the work is being done in the codebehind. Thanks anyway.

        B 1 Reply Last reply
        0
        • M munklefish

          Hi, I have a populated DataTable of vaying columns and rows. I need to somehow take the data of the 1st row and make this the column name of the column in which it is found eg; Column1 | Column2 | Column3 name | date | email mike | 26 June | me@you.com dave | 05 Apr | him@wibble.co.uk should read as: name | date | email mike | 26 June | me@you.com dave | 05 Apr | him@wibble.co.uk Any suggestions? Many Thanks. :^)

          V Offline
          V Offline
          Vimalsoft Pty Ltd
          wrote on last edited by
          #4

          Good Afternoon Step 1

          --Example Table
          create table MYT
          (
          Column1 varchar(30) null,
          Column2 varchar(30) null,
          Column3 varchar(30) null
          )

          i made some inserts in the tables like this

          insert into MYT
          values('mike','26 June','me@yahoo.com')

          and i had this

          --Assign Variables
          DECLARE @Col1 varchar(15)
          set @Col1 = (select top 1Column1 from MYT)
          --rename the Column
          exec sp_rename 'myT.Column1',@Col1
          go
          --Assign Variables
          DECLARE @COL2 varchar(15)
          set @Col2 = (select top 1 Column2 from MYT)
          --rename the Column
          exec sp_rename 'myT.Column2',@Col2
          go
          --Assign Variables
          DECLARE @Col3 varchar(15)
          set @Col3 = (select top 1 Column3 from MYT)
          --rename the Column
          exec sp_rename 'myT.Column3',@Col3

          and the Tale looked like this

          name date email

          name date email
          mike 26 June me@yahoo.com

          as you can see the Columns are still in the table , Delete the first row. hope it Helps Vuyiswa Maseko

          Vuyiswa Maseko, Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers." C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.somee.com http://www.vuyiswamaseko.tiyaneProperties.co.za vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

          M 1 Reply Last reply
          0
          • M munklefish

            That doesnt solve the problem unfortunately. As explained i need to take the content of the cell in row1 and make that the column name. This is because i dont know the content of row1 nor do i know how many columns there are, since the info is from an uploaded source. Likewise i dont have a gridview, i have a datatable as the work is being done in the codebehind. Thanks anyway.

            B Offline
            B Offline
            Baran M
            wrote on last edited by
            #5

            In GridView1_RowDataBound, if (e.Row.RowType == DataControlRowType.Header) { e.Row.Visible = false; } if (e.Row.RowIndex == 0) { e.Row.RowType = DataControlRowType.Header; } hope this will help you.

            Education is not a way to escape poverty — it is a way of fighting it.

            M 1 Reply Last reply
            0
            • V Vimalsoft Pty Ltd

              Good Afternoon Step 1

              --Example Table
              create table MYT
              (
              Column1 varchar(30) null,
              Column2 varchar(30) null,
              Column3 varchar(30) null
              )

              i made some inserts in the tables like this

              insert into MYT
              values('mike','26 June','me@yahoo.com')

              and i had this

              --Assign Variables
              DECLARE @Col1 varchar(15)
              set @Col1 = (select top 1Column1 from MYT)
              --rename the Column
              exec sp_rename 'myT.Column1',@Col1
              go
              --Assign Variables
              DECLARE @COL2 varchar(15)
              set @Col2 = (select top 1 Column2 from MYT)
              --rename the Column
              exec sp_rename 'myT.Column2',@Col2
              go
              --Assign Variables
              DECLARE @Col3 varchar(15)
              set @Col3 = (select top 1 Column3 from MYT)
              --rename the Column
              exec sp_rename 'myT.Column3',@Col3

              and the Tale looked like this

              name date email

              name date email
              mike 26 June me@yahoo.com

              as you can see the Columns are still in the table , Delete the first row. hope it Helps Vuyiswa Maseko

              Vuyiswa Maseko, Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers." C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.somee.com http://www.vuyiswamaseko.tiyaneProperties.co.za vuyiswa@its.co.za http://www.itsabacus.co.za/itsabacus/

              M Offline
              M Offline
              munklefish
              wrote on last edited by
              #6

              Vuyiswa, Thanks but unfortunately i need to do this in the codebehind before uploading the DataTable to a SQL2005 server since i need to do some columnmapping via SQLBulkCopy. So i really need a solution which is able to work in the c# codebehind and programmatically perform the changes to the DataTable. Thanks.

              1 Reply Last reply
              0
              • M munklefish

                Hi, I have a populated DataTable of vaying columns and rows. I need to somehow take the data of the 1st row and make this the column name of the column in which it is found eg; Column1 | Column2 | Column3 name | date | email mike | 26 June | me@you.com dave | 05 Apr | him@wibble.co.uk should read as: name | date | email mike | 26 June | me@you.com dave | 05 Apr | him@wibble.co.uk Any suggestions? Many Thanks. :^)

                H Offline
                H Offline
                himanshu2561
                wrote on last edited by
                #7

                Hi Try this

                    for (int i = 0; i < dt.Columns.Count ; i++)
                    {
                        dt.Columns\[i\].ColumnName = dt.Rows\[0\]\[i\].ToString();
                    }
                    dt.Rows.RemoveAt(0);
                

                himanshu

                M 1 Reply Last reply
                0
                • H himanshu2561

                  Hi Try this

                      for (int i = 0; i < dt.Columns.Count ; i++)
                      {
                          dt.Columns\[i\].ColumnName = dt.Rows\[0\]\[i\].ToString();
                      }
                      dt.Rows.RemoveAt(0);
                  

                  himanshu

                  M Offline
                  M Offline
                  munklefish
                  wrote on last edited by
                  #8

                  Himanshu!!!!! Yet again you are a genius and an amazingly helpful guy. (i just hope this works now :laugh: :laugh: ) Thanks!

                  H 1 Reply Last reply
                  0
                  • M munklefish

                    Himanshu!!!!! Yet again you are a genius and an amazingly helpful guy. (i just hope this works now :laugh: :laugh: ) Thanks!

                    H Offline
                    H Offline
                    himanshu2561
                    wrote on last edited by
                    #9

                    thanx

                    himanshu

                    1 Reply Last reply
                    0
                    • B Baran M

                      In GridView1_RowDataBound, if (e.Row.RowType == DataControlRowType.Header) { e.Row.Visible = false; } if (e.Row.RowIndex == 0) { e.Row.RowType = DataControlRowType.Header; } hope this will help you.

                      Education is not a way to escape poverty — it is a way of fighting it.

                      M Offline
                      M Offline
                      munklefish
                      wrote on last edited by
                      #10

                      Hi still someway off what im asking help with but thanks anyway.

                      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