简体   繁体   English

像C#中的自定义集合一样的数据表

[英]Datatable like custom collection in c#

In theory how would I do this. 从理论上讲,我该怎么做。

short winded: store data like a datatable using custom collections, having variable amount of fields and columns...so long as the rows are consistent. 短期:使用自定义集合存储数据,如数据表,具有可变数量的字段和列...只要行是一致的。

Long winded: wind:

2 or 3 classes: field, row, optionally: table 2或3类:字段,行,可选:表格

Normally I would do something like List<Person> myList = new List<Person>; 通常我会做类似List<Person> myList = new List<Person>;事情List<Person> myList = new List<Person>; Then that list could be bound to a datagridview and the columns would be based off the properties of the Person class. 然后,该列表可以绑定到datagridview,并且这些列将基于Person类的属性。

Code to look at: 代码看一下:

List<row> table = new List<row>;
List<field> row0 = new List<field>;
row0.Add(new field(col1,"value1"));
row0.Add(new field(col2,"value2"));
row0.add(new field(col3,"value3"));
table.Add(row0);


dataGridView1.DataSource = table;

theoretical Output: 理论输出:

|    |col 1 | col 2| col 3|
___________________________
|row0|value1|value2|value3|



public class cField
{
    public string Name { get; set; }
    public string Content { get; set; }
    public cField()
    {
    }

    public cField(string name, string content)
    {
        Name = name;
        Content = content;
    }
}

public class cRow:BindingList<cField>
{
    public cRow()
    {
    }
}
public class tables:BindingList<cRow>
{

    public tables()
    {
        fillTestData();
    }

    private void fillTestData()
    {
        for (Int32 i = 0; i < 10; i++)
        {
            cRow tRow = new cRow();

                for (Int32 x=0; x < 3; x++)
                {
                    cField f1 = new cField("ColumnName" + x.ToString(), "content" + x.ToString());
                    tRow.Add(f1);
                }
                base.Items.Add(tRow);
        }                        
    }
}

//example class which shows the output of what I'd like.
public class eField
{
    public string ColumnName0 { get; set; }
    public string ColumnName1 { get; set; }
    public string ColumnName2 { get; set; }

    public eField(string colName0, string colName1, string colName2)
    {
        ColumnName0 = colName0;
        ColumnName1 = colName1;
        ColumnName2 = colName2;
    }
}

public class eTable : BindingList<eField>
{
    public eTable()
    {
        base.Add (new eField ("content","content", "content"));
        base.Add(new eField("content", "content", "content"));
        base.Add(new eField("content", "content", "content"));
    }
}

Now Here is code for the form. 现在,这里是表单的代码。

public partial class Form1 : Form
{

    tables t;

    public Form1()
    {
        InitializeComponent();


    }

    private void Form1_Load(object sender, EventArgs e)
    {
        t = new tables ();

        dataGridView1.DataSource = t;

        dataGridView2.DataSource = t[0];

        eTable table3 = new eTable ();

        dataGridView3.DataSource = table3;

    }
}

If you make that code into a project...you will see the first binding....pulls some built in stuff from the bindinglist into grid1. 如果将代码放入项目中,您将看到第一个绑定。...将一些内置的东西从绑定列表中拉到grid1中。 Grid2 lists my fields vertically when I want them horizontal. 当我希望水平时,Grid2垂直列出我的字段。

Grid 3 shows exactly how I want my output to be.....yet I can't achieve it with the collection structure I have going to mimic a dataTable....(provided in code) 网格3确切地显示了我希望我的输出如何.....但是我无法通过模仿dataTable的集合结构来实现它。...(代码提供)

Disclaimer: I am short on keywords I would need to research this problem. 免责声明:我缺少研究此问题所需的关键字。 I didn't find much. 我没找到太多。 The closest thing I found was related to linq and pivots. 我发现最接近的东西与linq和ivot有关。 But non of their outputs seemed to be as I described. 但是他们的产出似乎都不像我描述的那样。

I use custom collections all over the place, so I would like to keep my code very similar instead of using a datatable. 我到处都使用自定义集合,因此我想使代码保持非常相似,而不是使用数据表。 This is the first time I have needed my collections to behave in this manner. 这是我第一次需要我的收藏以这种方式运行。

It sounds like you are looking for a collection of objects to use in memory once you have loaded the data from a database. 听起来好像您正在寻找从数据库中加载数据后要在内存中使用的对象的集合。 You can do calculations and the like on the built-in System.Data objects, but it is cumbersome, and it does not perform well with a large amount of data. 您可以在内置的System.Data对象上执行计算等操作,但是它很麻烦,并且在处理大量数据时效果不佳。

We use System.Data objects heavily to present data. 我们大量使用System.Data对象来呈现数据。 We try to do calculations in the database later and present the results as a DataSet, so the client doesn't have to do any data manipulation. 我们稍后尝试在数据库中进行计算,并将结果显示为DataSet,因此客户端不必进行任何数据操作。

A few of our modules need more sophisticated data processing. 我们的一些模块需要更复杂的数据处理。 In one case, we used an array of objects that represented a large amount of data to be massaged on the fly. 在一种情况下,我们使用了一组对象,这些对象表示要动态处理的大量数据。 The columns were fixed, so they were easy to implement as properties on each object. 列是固定的,因此很容易将它们实现为每个对象的属性。 When the app presented this data, it generated a small summary DataSet to be displayed in a grid. 当应用程序显示此数据时,它会生成一个小的摘要数据集,以显示在网格中。

We have another module in which there are fields that can have values, or they can also have calculations based on other fields. 我们还有另一个模块,其中的某些字段可以具有值,或者它们也可以基于其他字段进行计算。 For this model, we opted to use objects that have dependencies on other objects that made a sort of web of calculations. 对于此模型,我们选择使用对其他进行某种计算的对象具有依赖性的对象。 Change one value, and the ValueChanged event notifies any dependent fields that they need to be calculated, which changes those values, etc. (This is a gross simplification.) 更改一个值,然后ValueChanged事件通知所有需要计算的相关字段,这些相关字段将更改这些值,等等。(这是总的简化。)

If I had to present a variable number of columns, I'd seriously consider sticking with a System.Data.DataSet. 如果必须提供可变数量的列,我将认真考虑坚持使用System.Data.DataSet。 If that really doesn't work for you, you might consider a hashtable that maps a column name to a collection of row values for that column. 如果这对您确实不起作用,则可以考虑使用一个哈希表,该哈希表将列名映射到该列的行值的集合。 I believe that is how the System.Data.DataTable is implemented; 我相信这就是System.Data.DataTable的实现方式。 it stores values by column, not by row. 它按列而不是按行存储值。 Then a row object would know its row index and how to grab the values out of the column collections. 然后,行对象将知道其行索引以及如何从列集合中获取值。

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

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