C# : How to add data data from a ListView control to a Dictionary
The list view is like
i need to add the data from the Listview
to a Dictionary<String,String>
.
Now i'm using for loop
to do this. Is there any wa开发者_开发百科y to do this using LINQ
.
Finally the Dictionary will contain
{"A","1"}
{"B","2"}
{"C","3"}
EDIT My code :
Dictionary<String, String> Dic = new Dictionary<string, string>();
for (int i = 0; i < listView1.Items.Count; i++)
{
Dic.Add(listView1.Items[i].Text.ToString(), listView1.Items[i].SubItems[1].Text.ToString());
}
Thanks in advance
Based on your sample:
Dictionary<String, String> Dic = listView.Items
.Cast<ListViewItem>()
.ToDictionary(x => x.Text, x => x.SubItems[0].Text);
精彩评论