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. dynamically adding columns to datagrid from sql statement

dynamically adding columns to datagrid from sql statement

Scheduled Pinned Locked Moved ASP.NET
databasehelptutorialquestionlounge
4 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.
  • G Offline
    G Offline
    gbabu17
    wrote on last edited by
    #1

    hi, i have seen some of the datagrid samples in internet. In most of the cases the datagrid is bind to some table. But if i want to display the output of an sql query(contains select statement with n number of columns) in Datagrid, i can simply use AutogenerateColumns=true and populate the datagrid without having any bound columns. That is fine. If i want to add some more extra columns like a checkbox column and an edit link button column i can do it using property builder. But,the problem is how can i order the columns in datagrid. for example: 1st col: checkboxes 2nd-4th col : from sql statemnt 5th col : edit link button Also how can i make a random column from sql statement to a hyperlink column. thanks

    R 1 Reply Last reply
    0
    • G gbabu17

      hi, i have seen some of the datagrid samples in internet. In most of the cases the datagrid is bind to some table. But if i want to display the output of an sql query(contains select statement with n number of columns) in Datagrid, i can simply use AutogenerateColumns=true and populate the datagrid without having any bound columns. That is fine. If i want to add some more extra columns like a checkbox column and an edit link button column i can do it using property builder. But,the problem is how can i order the columns in datagrid. for example: 1st col: checkboxes 2nd-4th col : from sql statemnt 5th col : edit link button Also how can i make a random column from sql statement to a hyperlink column. thanks

      R Offline
      R Offline
      Ramasubramaniam
      wrote on last edited by
      #2

      One simplest way is, add few temprory columns in the query, wherever you want add column in the grid. Then bind that query result to the datagrid. Example: ======== Actual query is, select column1, column2, column3 from table1 Imagine you want to add columns in between 1 and 2, 2 and 3 then the query should be, select column1, '' as tempcol1, column2, '' as tempcol2, column3 from table1 then on the itemdatabound event you can add any control like (button, linkbutton, or dropdownlist) on the column. Ram

      G 1 Reply Last reply
      0
      • R Ramasubramaniam

        One simplest way is, add few temprory columns in the query, wherever you want add column in the grid. Then bind that query result to the datagrid. Example: ======== Actual query is, select column1, column2, column3 from table1 Imagine you want to add columns in between 1 and 2, 2 and 3 then the query should be, select column1, '' as tempcol1, column2, '' as tempcol2, column3 from table1 then on the itemdatabound event you can add any control like (button, linkbutton, or dropdownlist) on the column. Ram

        G Offline
        G Offline
        gbabu17
        wrote on last edited by
        #3

        can you please explain a bit about the modified query.

        R 1 Reply Last reply
        0
        • G gbabu17

          can you please explain a bit about the modified query.

          R Offline
          R Offline
          Ramasubramaniam
          wrote on last edited by
          #4

          Example: ======== Actual query is, select column1, column2, column3 from table1 Imagine you want to add columns in between 1 and 2, 2 and 3 then the query should be, select column1, '' as tempcol1, column2, '' as tempcol2, column3 from table1 then on the itemdatabound event you can add any control like (button, linkbutton, or dropdownlist) on the column Explanation: ============ You just execute the these two queries. First query will return three columns, second query will return five columns. The temp columns are not available in any table, which will be used to create a column in the grid. You just execute this queries in the query analyzer and check. Ram

          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