从数据表到绑定列表

从数据表到绑定列表

问题描述:

我正在从DataTable切换到BindingList。我将DataTable绑定到DataGrid对象。

I am in the process of switching over from DataTable to BindingList. I bind the DataTable to the DataGrid object.

这是我的两难境地:虽然我当然看到了切换的好处,但我的情况会使它变得有些复杂,我想知道

Here my dilemma: while I certainly see the advantages of switching over, my circumstances will make it a bit complex and I am wondering whether it is worth it to switch over.

我的场景:
我有一个显示化学样品的DataGrid。共有5种样本类型,其中每种类型的网格中的列都会有所不同(有时会基于其他参数而处于同一类型内)。有些列保持不变,有些以3种类型出现,有些以4种类型出现,依此类推。这不完全是简单继承的情况。当前,我有一个DataTable,它很容易,因为我可以加载我想要的任何列,而不必具有每种样本类型的属性,这是使用BindingList时必须要做的。但是,使用BindingList可以更轻松地引用示例属性。

My scenario: I have a DataGrid which displays chemical samples. There are 5 kinds of sample type, where the columns in the grid will differ for each type (and sometimes within the same type based on other parameters). Some columns remain the same, some are present in 3 types, some in 4 etc etc. Not exactly a case of simple inheritance. Currently, I have a DataTable which makes it easy since I can load whichever columns I want without having to have properties for each sample type which is what I will have to do if I use BindingList. But then, BindingList will make it much easier down the road to refer to sample properties.

这是我第一次使用此对象(BindingList),因此欢迎提出任何建议。据我所知,BindingList绑定到属性。因此,如果我有:

This is my first experience with this object (BindingList) so any suggestions welcome. From what I know BindingList binds to properties. So if I have:

public class Sample
{
    protected string m_seq;
    protected string m_id;
    protected string m_weight;
    protected string m_units;

    public Sample()
    {
    }

    public string Seq
    {
        get { return m_seq; }
        set { m_seq = value; }
    }

    public string Id
    {
        get { return m_id; }
        set { m_id = value; }
    }

    public string Weight
    {
        get { return m_weight; }
        set { m_weight = value; }
    }

    public string Units
    {
        get { return m_units; }
        set { m_units = value; }
    }

} //end of class

如果我有

BindingList samples = new BindingList<Sample>();

这意味着我将要拥有6种类型的样本,所有样本都定义了属性。最终的继承结构可能很复杂,所以我想知道这是否值得。欢迎任何反馈。

Which means I am going to have 6 types of samples, all with properties defined. The final inheritance structure may be complex, so I am wondering whether it is worth it at all. Any feedback is welcome.

我正在使用c#2.0;

I am using c# 2.0; it is a Windows App.

如果您使用的是 DataGrid ,有没有机会切换到 DataGridView ?它将更加灵活...

If you are using DataGrid, is there any chance you could switch to DataGridView? It will be far more flexible...

我不确定这个问题,但是:

I'm not entirely sure from the question, but:

然后只需设置 AutoGenerateColumns = false 在添加数据之前,并自己添加所需的列;或者,将其保持启用状态,并在您不想看到的列上简单地设置 .Visible = false

Then simply set AutoGenerateColumns = false before you add the data, and add the desired columns yourself; or alternatively, leave it enabled and simply set .Visible = false on the columns you don't want to see.

然后就会变得棘手;您将进入 System.ComponentModel 的黑暗角落;您可以使用 DataGridView 通过 ITypedList (每个列表实例允许不同的定义)或来完成此操作TypeDescriptionProvider (允许每种类型或每个列表实例使用不同的定义)-但是,只有 TypeDescriptionProvider 可以与 BindingList&lt ; T> 。老实说,我不认为这是您的意思;如果是的话,坚持使用 DataTable 会更简单(两者都很难正确实现)。

Then it gets tricky; you're getting into the darker corners of System.ComponentModel; you can do this with DataGridView via ITypedList (which allows different definitions per list instance) or TypeDescriptionProvider (which allows different definitions per type or per list instance) - but of those, only TypeDescriptionProvider will work with BindingList<T>. To be honest, I don't think this is what you mean; and if it is, it would be simpler to stick with DataTable (both are very hard to implement correctly).

我希望能有所帮助;我希望它就像隐藏一些列一样简单!如果我错过了要点,请告诉我。

I hope that helps; and I hope it is as simple as hiding some columns! If I've missed the point, let me know.