开发者

How to bind List<> to a column in ListView

开发者 https://www.devze.com 2023-01-14 01:09 出处:网络
I\'ve got a DataTable with this fields datatable.Columns.Add(\"ProductID\", typeof(int)); datatable.Columns.Add(\"LocationList\", typeof(List<string>));

I've got a DataTable with this fields

datatable.Columns.Add("ProductID", typeof(int));
datatable.Columns.Add("LocationList", typeof(List<string>));
datatable.Columns.Add("LocationIds", typeof(List<int>));
datatable.Columns.Add("ProductName"开发者_运维百科, typeof(string));
datatable.Columns.Add("Brand", typeof(string));
datatable.Columns.Add("Price", typeof(decimal));
datatable.Columns.Add("Quantity", typeof(int));
datatable.Columns.Add("Locations", typeof(string));

And I bind it to a ListView

foreach (DataRow row in productsDataTable.Rows)
{
    var item = new ListViewItem(row[0].ToString());
    for (var i = 1; i < productsDataTable.Columns.Count; i++)
        item.SubItems.Add(row[i].ToString());

    lvSearchResults.Items.Add(item);
}

I want to bind the List<> fields, so that when a row is selected, I'd be able to get the data from the Lists and do some calculations with it. Is there any way to do this?


The ListViewItem has a Tag property that you can store data in to retrieve and use later, for example:

foreach (DataRow row in productsDataTable.Rows)
{
    var item = new ListViewItem(row[0].ToString());

    // Store the specific values you want to later retrieve
    item.Tag = new object[] { row["LocationList"], row["LocationIds"] };

    // Or, store the whole row
    item.Tag = row;

    lvSearchResults.Items.Add(item);
}


The ListViewItem.Tag property allows you to associate anything you want with the item. You could store the entire DataRow in there and retrieve any columns you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号