将WPF datagrid源设置为一组字符串数组 [英] Setting WPF datagrid source to a group of string arrays
问题描述
我可以想到的简单选项是循环遍历数组,选出值并将其作为行添加到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屋!