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. General Programming
  3. C#
  4. Populate Listview using LINQ

Populate Listview using LINQ

Scheduled Pinned Locked Moved C#
databasehelpquestioncsharpsql-server
7 Posts 6 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.
  • K Offline
    K Offline
    klaydze
    wrote on last edited by
    #1

    Hi, I'm trying to populate a listview control and my query is LINQ. I used one of the table (Production.Product) of AdventureWorks database. At first i have problem populating the header, but i figured it out after an hour. The code below is the code on how to populate column in listview using LINQ. Code:

    //Sample result when you query using SQL Server.

    ProductID Name ProductNumber MakeFlag FinishedGoodsFlag Color

    317 LL Crankarm CA-5965 0 1 Black
    318 ML Crankarm CA-5938 0 1 Black
    319 HL Crankarm CA-5990 0 1 Black
    320 Chainring CA-5932 0 1 Black

    //lvw = ListView control

    DataClasses1DataContext db = new DataClasses1DataContext();

    DataTable dt = new DataTable();
    DataColumn dc = new DataColumn();
    DataRow dr;

    var products = from p in db.Products
    where p.Color != null
    select p;

    #region getHeader
    var headers = products.First();
    foreach (var columnHeader in headers.GetType().GetProperties())
    {
    lvw.Columns.Add(columnHeader.Name);
    }
    #endregion

    Now, my problem is how do i populate the result of my query and add it to listview. Actually, i can populate it like this,

    ListViewItem lstItem = null;

    foreach (var itms in products)
    {
    lstItem = new ListViewItem(itms.ProductID.ToString());
    lstItem.SubItems.Add(itms.Name);
    lstItem.SubItems.Add(itms.ProductNumber);
    ..... and so on
    }

    The problem on the above code is that, what if i have a 25 columns in my query result? I have to type in my code 25x the lstItem.SubItems.Add(value) which kinda hassle for me. I want my function in populating the listview to be dynamic. :) Anyone out there can help me to solve my "simple (i think)" problem. Thanks and regards Jessie

    if(you type your code here) { Messagebox.Show("You help me a lot!"); } else { You help me = null; }

    M 1 Reply Last reply
    0
    • K klaydze

      Hi, I'm trying to populate a listview control and my query is LINQ. I used one of the table (Production.Product) of AdventureWorks database. At first i have problem populating the header, but i figured it out after an hour. The code below is the code on how to populate column in listview using LINQ. Code:

      //Sample result when you query using SQL Server.

      ProductID Name ProductNumber MakeFlag FinishedGoodsFlag Color

      317 LL Crankarm CA-5965 0 1 Black
      318 ML Crankarm CA-5938 0 1 Black
      319 HL Crankarm CA-5990 0 1 Black
      320 Chainring CA-5932 0 1 Black

      //lvw = ListView control

      DataClasses1DataContext db = new DataClasses1DataContext();

      DataTable dt = new DataTable();
      DataColumn dc = new DataColumn();
      DataRow dr;

      var products = from p in db.Products
      where p.Color != null
      select p;

      #region getHeader
      var headers = products.First();
      foreach (var columnHeader in headers.GetType().GetProperties())
      {
      lvw.Columns.Add(columnHeader.Name);
      }
      #endregion

      Now, my problem is how do i populate the result of my query and add it to listview. Actually, i can populate it like this,

      ListViewItem lstItem = null;

      foreach (var itms in products)
      {
      lstItem = new ListViewItem(itms.ProductID.ToString());
      lstItem.SubItems.Add(itms.Name);
      lstItem.SubItems.Add(itms.ProductNumber);
      ..... and so on
      }

      The problem on the above code is that, what if i have a 25 columns in my query result? I have to type in my code 25x the lstItem.SubItems.Add(value) which kinda hassle for me. I want my function in populating the listview to be dynamic. :) Anyone out there can help me to solve my "simple (i think)" problem. Thanks and regards Jessie

      if(you type your code here) { Messagebox.Show("You help me a lot!"); } else { You help me = null; }

      M Offline
      M Offline
      Mario Majcica
      wrote on last edited by
      #2

      ListView is quite limiting control. You can take a look on this extended ListView control A Much Easier to Use ListView[^] that supports data binding. Hope it helps. M.

      realJSOPR K 2 Replies Last reply
      0
      • M Mario Majcica

        ListView is quite limiting control. You can take a look on this extended ListView control A Much Easier to Use ListView[^] that supports data binding. Hope it helps. M.

        realJSOPR Offline
        realJSOPR Offline
        realJSOP
        wrote on last edited by
        #3

        Wow - that article has 453 5 votes, yet only 56 downloads.

        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
        -----
        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
        -----
        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

        M D N 3 Replies Last reply
        0
        • realJSOPR realJSOP

          Wow - that article has 453 5 votes, yet only 56 downloads.

          ".45 ACP - because shooting twice is just silly" - JSOP, 2010
          -----
          You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
          -----
          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          As soon as I saw you response I knew the article, it is an excellent article but a hugely complex control. I played with it but found it too complex for my requirements. Mind you I also trashed Infragistics for the same reason!

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          • M Mario Majcica

            ListView is quite limiting control. You can take a look on this extended ListView control A Much Easier to Use ListView[^] that supports data binding. Hope it helps. M.

            K Offline
            K Offline
            klaydze
            wrote on last edited by
            #5

            thanks for the link. maybe ill try it later. cheers!

            if(you type your code here) { Messagebox.Show("You help me a lot!"); } else { You help me = null; }

            1 Reply Last reply
            0
            • realJSOPR realJSOP

              Wow - that article has 453 5 votes, yet only 56 downloads.

              ".45 ACP - because shooting twice is just silly" - JSOP, 2010
              -----
              You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
              -----
              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

              D Offline
              D Offline
              Dan Mos
              wrote on last edited by
              #6

              I have no idea how the hamsters track the downloads but I guess that the low number of downloads might be caused by the fact that the download is just a link to a sourceforge.net download?:~

              All the best, Dan

              1 Reply Last reply
              0
              • realJSOPR realJSOP

                Wow - that article has 453 5 votes, yet only 56 downloads.

                ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                -----
                You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                -----
                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                N Offline
                N Offline
                Not Active
                wrote on last edited by
                #7

                With the Browse Code tab it isn't really necessary to download the code to learn what it does and how it does it. That may explain the low numbers.


                I know the language. I've read a book. - _Madmatt

                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