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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Binding List to GridView

Binding List to GridView

Scheduled Pinned Locked Moved ASP.NET
databasewpfwcftutorial
6 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.
  • S Offline
    S Offline
    sangramkp
    wrote on last edited by
    #1

    Hi, I am Using a List to store the data retrived from database... My code goes as this Service Layer Method: public List GetCities() { DataSet ds= cdb.GetCities(); List city = new List(); foreach (DataRow row in ds.Tables[0].Rows) { city.Add(new City(row["city_id"].ToString(), row["city_name"].ToString(), row["state_id"].ToString(), row["city_classification_id"].ToString())); } return city; } In Application Layer: I am Caliing the method as below GridView1.DataSource = cs.GetCities(); GridView1.DataBind(); It works fine but .... The Columns are not coming in the same order as in Database i.e... CityID, CityName, StateID, cityClassificationID. The column order comes in as cityClassificationID,CityId, CityName, StateID. But I want them in the same Order i.e CityID, CityName, StateID, CityClassificationID... How to accomplish this....:rose:

    Sangram (A battle with self) Life is simple, we are the ones makes the living difficult

    C N 2 Replies Last reply
    0
    • S sangramkp

      Hi, I am Using a List to store the data retrived from database... My code goes as this Service Layer Method: public List GetCities() { DataSet ds= cdb.GetCities(); List city = new List(); foreach (DataRow row in ds.Tables[0].Rows) { city.Add(new City(row["city_id"].ToString(), row["city_name"].ToString(), row["state_id"].ToString(), row["city_classification_id"].ToString())); } return city; } In Application Layer: I am Caliing the method as below GridView1.DataSource = cs.GetCities(); GridView1.DataBind(); It works fine but .... The Columns are not coming in the same order as in Database i.e... CityID, CityName, StateID, cityClassificationID. The column order comes in as cityClassificationID,CityId, CityName, StateID. But I want them in the same Order i.e CityID, CityName, StateID, CityClassificationID... How to accomplish this....:rose:

      Sangram (A battle with self) Life is simple, we are the ones makes the living difficult

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Turn off auto columns and define them yourself in the order you want.

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      1 Reply Last reply
      0
      • S sangramkp

        Hi, I am Using a List to store the data retrived from database... My code goes as this Service Layer Method: public List GetCities() { DataSet ds= cdb.GetCities(); List city = new List(); foreach (DataRow row in ds.Tables[0].Rows) { city.Add(new City(row["city_id"].ToString(), row["city_name"].ToString(), row["state_id"].ToString(), row["city_classification_id"].ToString())); } return city; } In Application Layer: I am Caliing the method as below GridView1.DataSource = cs.GetCities(); GridView1.DataBind(); It works fine but .... The Columns are not coming in the same order as in Database i.e... CityID, CityName, StateID, cityClassificationID. The column order comes in as cityClassificationID,CityId, CityName, StateID. But I want them in the same Order i.e CityID, CityName, StateID, CityClassificationID... How to accomplish this....:rose:

        Sangram (A battle with self) Life is simple, we are the ones makes the living difficult

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        Strange, Why don't you return the DataTable object from dataset ? Any way you are iterating through all rows, or is there any other reason that you are specific with List ?


        My Website | Ask smart questions

        C 1 Reply Last reply
        0
        • N N a v a n e e t h

          Strange, Why don't you return the DataTable object from dataset ? Any way you are iterating through all rows, or is there any other reason that you are specific with List ?


          My Website | Ask smart questions

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          There is a school of thought that suggests the data layer should return collections of objects instead of database related classes.

          Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          N 1 Reply Last reply
          0
          • C Christian Graus

            There is a school of thought that suggests the data layer should return collections of objects instead of database related classes.

            Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            Christian Graus wrote:

            There is a school of thought that suggests the data layer should return collections of objects instead of database related classes.

            Great information. I was not knowing. What will be the impact when it returns Database classes ? upto my knowledge, DataTable is also a collection class which contain a collection of DataRows. So what is making difference ?


            My Website | Ask smart questions

            C 1 Reply Last reply
            0
            • N N a v a n e e t h

              Christian Graus wrote:

              There is a school of thought that suggests the data layer should return collections of objects instead of database related classes.

              Great information. I was not knowing. What will be the impact when it returns Database classes ? upto my knowledge, DataTable is also a collection class which contain a collection of DataRows. So what is making difference ?


              My Website | Ask smart questions

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              He's returning a collection of entity objects. It's at least partially a religious discussion. I've seen all sorts of other OO principles thrown out in order to pursue this one goal. It's not clear cut, and you need to make a call based on the needs of your project. It is very sexy to call the data layer and get back a list of custom business objects, tho.

              Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              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