繁体   English   中英

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

[英]Datatable like custom collection in c#

从理论上讲,我该怎么做。

短期:使用自定义集合存储数据,如数据表,具有可变数量的字段和列...只要行是一致的。

wind:

2或3类:字段,行,可选:表格

通常我会做类似List<Person> myList = new List<Person>;事情List<Person> myList = new List<Person>; 然后,该列表可以绑定到datagridview,并且这些列将基于Person类的属性。

代码看一下:

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;

理论输出:

|    |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"));
    }
}

现在,这里是表单的代码。

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;

    }
}

如果将代码放入项目中,您将看到第一个绑定。...将一些内置的东西从绑定列表中拉到grid1中。 当我希望水平时,Grid2垂直列出我的字段。

网格3确切地显示了我希望我的输出如何.....但是我无法通过模仿dataTable的集合结构来实现它。...(代码提供)

免责声明:我缺少研究此问题所需的关键字。 我没找到太多。 我发现最接近的东西与linq和ivot有关。 但是他们的产出似乎都不像我描述的那样。

我到处都使用自定义集合,因此我想使代码保持非常相似,而不是使用数据表。 这是我第一次需要我的收藏以这种方式运行。

听起来好像您正在寻找从数据库中加载数据后要在内存中使用的对象的集合。 您可以在内置的System.Data对象上执行计算等操作,但是它很麻烦,并且在处理大量数据时效果不佳。

我们大量使用System.Data对象来呈现数据。 我们稍后尝试在数据库中进行计算,并将结果显示为DataSet,因此客户端不必进行任何数据操作。

我们的一些模块需要更复杂的数据处理。 在一种情况下,我们使用了一组对象,这些对象表示要动态处理的大量数据。 列是固定的,因此很容易将它们实现为每个对象的属性。 当应用程序显示此数据时,它会生成一个小的摘要数据集,以显示在网格中。

我们还有另一个模块,其中的某些字段可以具有值,或者它们也可以基于其他字段进行计算。 对于此模型,我们选择使用对其他进行某种计算的对象具有依赖性的对象。 更改一个值,然后ValueChanged事件通知所有需要计算的相关字段,这些相关字段将更改这些值,等等。(这是总的简化。)

如果必须提供可变数量的列,我将认真考虑坚持使用System.Data.DataSet。 如果这对您确实不起作用,则可以考虑使用一个哈希表,该哈希表将列名映射到该列的行值的集合。 我相信这就是System.Data.DataTable的实现方式。 它按列而不是按行存储值。 然后,行对象将知道其行索引以及如何从列集合中获取值。

暂无
暂无

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

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