从数据表到绑定列表
我正在从 DataTable 切换到 BindingList。 我将 DataTable 绑定到 DataGrid 对象。
我的困境是:虽然我当然看到了转换的优势,但我的情况会让它变得有点复杂,我想知道是否值得转换。
我的场景: 我有一个显示化学样品的数据网格。 有 5 种样本类型,其中每种类型的网格中的列都不同(有时基于其他参数在同一类型内)。 有些列保持不变,有些以 3 种类型存在,有些以 4 种类型等等。不完全是简单继承的情况。 目前,我有一个 DataTable,这使得它变得很容易,因为我可以加载我想要的任何列,而不必为每个样本类型提供属性,如果我使用 BindingList,我就必须这样做。 但是,BindingList 将使引用示例属性变得更加容易。
这是我第一次接触这个对象(BindingList),所以欢迎提出任何建议。 据我所知 BindingList 绑定到属性。 因此,如果我有:
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
如果我有类似的内容,网格将绑定到 Sample 类的属性
BindingList samples = new BindingList<Sample>();
这意味着我将拥有 6 种类型的样本,所有样本都定义了属性。 最终的继承结构可能很复杂,所以我想知道这是否值得。 欢迎任何反馈。
我正在使用c#2.0; 它是一个 Windows 应用程序。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您正在使用
DataGrid
,是否有机会切换到DataGridView
? 它将更加灵活......我对这个问题并不完全确定,但是:
如果属性是固定的,但您不想显示所有列
那么只需设置 AutoGenerateColumns = false 在添加数据之前,自行添加所需的列; 或者,将其保持启用状态,然后简单地在您不想看到的列上设置
.Visible = false
。如果属性在运行时是动态的
那么事情就会变得棘手; 你正在进入 System.ComponentModel 的黑暗角落; 您可以通过
ITypedList
(允许每个列表实例有不同的定义)或TypeDescriptionProvider
(允许每个类型或每个列表有不同的定义)使用DataGridView
来完成此操作实例) - 但其中只有TypeDescriptionProvider
可以与BindingList
配合使用。 老实说,我不认为这就是你的意思; 如果是的话,坚持使用 DataTable 会更简单(两者都很难正确实现)。我希望这会有所帮助; 我希望它就像隐藏一些列一样简单! 如果我错过了重点,请告诉我。
If you are using
DataGrid
, is there any chance you could switch toDataGridView
? 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 withDataGridView
viaITypedList
(which allows different definitions per list instance) orTypeDescriptionProvider
(which allows different definitions per type or per list instance) - but of those, onlyTypeDescriptionProvider
will work withBindingList<T>
. To be honest, I don't think this is what you mean; and if it is, it would be simpler to stick withDataTable
(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.