Linq result to Array of string >> Listview
-
Friday, June 22, 2012 8:10 AM
I want to fill listview (winform) from linq result. Actually this is not hard at all where all you need is 3 line of code to do it. for example
Dim db As NewmyDataContext
Dim query = From t In db.mytable Select t
For Each rs Inquery
Dim lvwitem = lvw2.Items.Add(rs.field_1)
lvwitem.SubItems.AddRange({rs.field_2,rs.field_3,rs.field_4})
Nextwhat I am trying to do is to create a general function that able to get the linq result as parameter and fill the listview from it. That require that I should not use fixed field name as I used above "field_1,field_2"
Is there a way of converting the linq result to array of string so they could be accepted is in the AddRange()?
or
Does any one have a general function to fill listview with any linq result without using the field name ?
Thanks in advance
- Edited by Samir Ibrahim Friday, June 22, 2012 8:11 AM
All Replies
-
Friday, June 22, 2012 9:52 AM
linq has methods ToArray, ToList for converting to array
Dim queryarray = (From t In db.mytable Select t).ToArray
KiranMP
-
Friday, June 22, 2012 10:40 AM
linq has methods ToArray, ToList for converting to array
Dim queryarray = (From t In db.mytable Select t).ToArray
KiranMP
That is true, but does not work well.
In order for me to iterate the result as you answered, I had to do something like
dim field1 = querryarray(0).field_1
dim field2 = querryarray(0).field_2as you can see in bold, I had to use "field/column name" in order to get the value
I even try to access it like querryarray(0)(0) where I thought the second (0) will fetch the value in column index(0) but it give error (it cannot be indexed because it has no default property).
I had tried .ToArray and .ToList and many many other and I am doing research since 2 days :)
-
Friday, June 22, 2012 1:58 PMhave you tried
listView.DataSource = query;
listView.DataBind();
KiranMP
-
Friday, June 22, 2012 2:03 PM
Hi Kiran
That exist for web asp.net not for listview control for (winform)
I wish they made one.
-
Friday, June 22, 2012 4:51 PM
I think if you select specific columns in linq query then it will allow you to bind result to listview
From t In db.mytable Select new[]{t.field_1,t..field_2,t.field_1}
KiranMP
-
Monday, June 25, 2012 8:11 AMModerator
Hi Samir,
Seems like this ListView control for winform has a little different way to binding data, you should declare a ListViewItem variable first and then add the data into its Subitems collection which will hold the record for you. If you want to get more detailed information about this control, please go through the following link and its example code snippet:
Here is a piece of code for this issue:
db = new NorthwindDataContext(); var result = from c in db.Customers where c.CustomerID == "AROUT" select new { c.CustomerID, c.ContactName, c.City }; // Create three items and three sets of subitems for each item. ListViewItem item1 = new ListViewItem("item1",0); // Place a check mark next to the item. item1.Checked = true; //item1.SubItems.Add("1"); //item1.SubItems.Add("2"); //item1.SubItems.Add("3"); var kk = result.ToList(); item1.SubItems.Add(kk[0].CustomerID); item1.SubItems.Add(kk[0].ContactName); item1.SubItems.Add(kk[0].City);
Hope this could help you.
Tony Xiao [MSFT]
MSDN Community Support | Feedback to us
-
Monday, June 25, 2012 9:49 AM
Hi Tony & Kiran
Let me say that forcing you to think in my requirement of converting the
linq result to array of string has the fact that in listview I can do something
like that (assume listview with 5 columns)
Dim ar() As String = {"0","1","2","3","4"}
ListView1.Items.Add(new ListViewItem(ar))As you can see above, in one line I filled the
listview, that is why I was asking if I can convert the linq result to array of string.Anyway, let me give you the REAL thing I am after.
Here is how I can fill listview from datatable (note that i did not used any
field name, column name)Sub FillListview_by_datatable(ByVal lvw As ListView,ByVal dt As DataTable)
lvw.Clear
For Each col As DataColumn In dt.Columns
lvw.Columns.Add(col.ColumnName)
Next
For i = 0 to dt.Rows.Count -1
Dim lvwitem As ListViewItem = lvw.Items.Add(dt.Rows(i)(0))
For j = 1 to dt.Columns.Count -1
lvwitem.SubItems.Add(dt.Rows(i)(j).ToString)
Next
Next
End Sub
By passing data table, I can fill the listview without knowing how
many columns without caring what are the field name.I want to do the same for linq result. How
- Edited by Samir Ibrahim Monday, June 25, 2012 9:50 AM
- Edited by Samir Ibrahim Monday, June 25, 2012 9:50 AM
-
Tuesday, June 26, 2012 8:58 AM
Well, what do you know :)
Here it is after google send me a msg "Please stop searching" lol
Dim db As New northwindDataContext Dim query = From t In db.Customers Select t FillListview_by_Linqresult(query.ToArray,lvwListView1) Sub FillListview_by_Linqresult (ByVal linq_result As Array,ByVal lvw As ListView) Dim col_count = linq_result(0).GetType.GetProperties().Count ' Add listview headers For i = 0 to col_count -1 Dim col_name = linq_result(0).GetType.GetProperties()(i).Name.ToString lvw.Columns.Add(col_name) Next i ' Fill listview with values For i = 0 to linq_result.Length - 1 Dim lvwitem as ListViewItem = lvw.Items.Add(linq_result(i).GetType.GetProperties()(0).GetValue(linq_result(i),Nothing)) For j = 1 to col_count -1 Dim sub_item = linq_result(i).GetType.GetProperties()(j).GetValue(linq_result(i),Nothing) If sub_item is Nothing then sub_item = "" sub_item = sub_item.ToString.Trim lvwitem.SubItems.Add(sub_item) Next j Next i End Sub
- Edited by Samir Ibrahim Tuesday, June 26, 2012 9:05 AM
- Edited by Samir Ibrahim Tuesday, June 26, 2012 1:33 PM

