Populate Listview using LINQ
-
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);
}
#endregionNow, 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; }
-
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);
}
#endregionNow, 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; }
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.
-
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.
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 -
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, 1997As 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
-
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.
-
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, 1997I 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
-
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, 1997With 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