From DataTable to BindingList

可紊 提交于 2019-12-24 10:22:47

问题


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.

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.

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

The grid will be bound to the properties of Sample class if I have something like

BindingList samples = new BindingList<Sample>();

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.

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


回答1:


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:

If the properties are fixed, but you don't want to display all the columns

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.

If the properties are dynamic at runtime

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.



来源:https://stackoverflow.com/questions/1173359/from-datatable-to-bindinglist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!