简体   繁体   English

C#列放入数组

[英]C# columns into array

I want to put each column into separate array. 我想将每一列放入单独的数组中。 So that I can later select the category from combobox and show values. 这样我以后可以从组合框中选择类别并显示值。 But I have no idea how to do this, I dont even know whether array is a good idea. 但是我不知道该怎么做,我什至不知道数组是否是一个好主意。 Thanks in advance. 提前致谢。

Category1    Category2       Category3
----------  ------------    ------------
  13            1              12  
  12            1              13
  12            5              14
  44            3              15
  23            6              16

I have tried something like this: 我已经尝试过这样的事情:

  if(char.IsNumber(s.Trim().ToCharArray().ElementAt(0)))
                {
                    double[,] p = new double[samples, columns];


                    for(int i=0;i<samples;i++)
                    {
                        for(int j=0;j<columns;j++)
                        {
                            //p[i,j]=
                        }
                    }
                }

The way I would do it is to first create a class: 我要做的方法是首先创建一个类:

public class Category
{
    public int Category1 {get;set;}
    public int Category2 {get;set;}
    public int Category3 {get;set;}
}

Then create a list and populate it: 然后创建一个列表并填充它:

var categories = new List<Category> { 
                           new Category { Category1 = 13, Category2 = 1, Category3 = 12 },
                           new Category { Category1 = 12, Category2 = 1, Category3 = 13 } 
    };

I'd just use list (unless the number of elements will remain the same and known) 我只使用列表(除非元素的数量保持相同且已知)
Also you have not mentioned where are your reading this list from. 另外,您还没有提到从何处读取此列表。 If its just a file convert it to some type of xml and parse it or just parse the text file. 如果只是一个文件,则将其转换为某种类型的xml并对其进行解析,或者仅对文本文件进行解析。 If db, then map the below classes to the db objects using ORM or just manually read them. 如果是db,则使用ORM将以下类映射到db对象,或者只是手动读取它们。

For your combo box 为您的组合框
Create a class and parse the data into this class objects 创建一个类并将数据解析为该类对象

class Category
{
  public string CategoryName {get; set;}
  public List<int> CategoryItems {get; set}
}

For combo box in your xaml have something like this 对于您的xaml中的组合框,请输入以下内容

<ComboBox ItemSource="{Binding Categories}"
          DisplayMemberPath="{Binding CategoryName}"
          SelectedItem="{Binding SelectedCategoryIndex}"/>
<ComboBox ItemSource="{Binding CategoryItems}"/>

In your viewmodel for the binding this is how you would have it 在绑定的视图模型中,这就是您要的样子

public int SelectedCategoryIndex {get; set;}
public List<Category> Categories {get; private set;}
public List<int> CategoryItems
{
  get
  {
    return this.Categories[SelectedCategoryIndex].CategoryItems;
  }
}

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM