将WPF datagrid源设置为一组字符串数组 [英] Setting WPF datagrid source to a group of string arrays

查看:178
本文介绍了将WPF datagrid源设置为一组字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多字符串数组。例如,13个用户名的数组,然后是13个密码的独立数组。有人可以告诉我,将这些进入WFP数据网格最有效的方法是?



我可以想到的简单选项是循环遍历数组,选出值并将其作为行添加到datagrid中,但我想知道是否可以通过数组作为列还是什么?



如果您需要更多信息,请通知我。

解决方案

DataGrid在属性(列)和项(行)概念上起作用。因此,数据结构如对象,数据表或XML的收集最适用于直观地加载数据ibnto DataGrid



使用普通值类型的数组,您必须将它们转换为数据结构。使用linq为您的优势...

  var consolidationList = 
arrayUserName.Select(
usrNm => ;
new {
UserName = usrName,
Password = arrayPasswords [arrayUserName.IndexOf(usrName)]
})ToList();

dataGrid.ItemsSource = consolidationList;

对于数组中的大量项目,列表生成将是缓慢的。在这种情况下,运行循环或使用PLINQ(在.Net 4.0的情况下)。


I have number of string arrays. For example an array of 13 usernames then a seperate array of 13 passwords. Could someone please tell me what the most efficient way of getting these into a WFP datagrid is?

The simple option I can think of is to loop through the arrays, pick out the values and add them as a row into the datagrid but I was wondering if I can pass the arrays in as columns or something?

Please let me know if you need anymore information.

解决方案

DataGrid works on attributes (columns) and items (rows) concept. So datastructures like collection of objects, data table or XML works best for loading data ibnto DataGrid intuitively.

With arrays of plain value types, you would have to convert them into a data structure. Use linq for your advantage...

 var consolidatedList = 
     arrayUserName.Select(
           usrNm => 
             new {
                   UserName = usrName, 
                   Password = arrayPasswords[arrayUserName.IndexOf(usrName)]
                 }).ToList();

 dataGrid.ItemsSource = consolidatedList;

Of couse the list generation would be slow for large number of items in the arrays. In such case run a loop or use PLINQ (in case of .Net 4.0).

这篇关于将WPF datagrid源设置为一组字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆