绑定到 BindingList- 选择绑定什么?
假设我有一个名为 Sample 的业务对象,并且有 Samples 的 BindingList。 一个样本有 4 个属性。
我可以选择将哪些属性绑定到 DataGrid 还是没有选项来自定义此类内容?
笔记: 我正在使用 Compact Framework,其中没有 DataGridView,以及 Autogenerate 属性和 DataMember 属性。
请在回复时牢记这一点。
Say I have an business object called Sample and I have BindingList of Samples. A sample has 4 properties.
Can I select which properties are bound to DataGrid or there no option to customize such a thing?
NOTE:
I am using Compact Framework, where is NO DataGridView, as well as Autogenerate property and DataMember property.
Please keep this in mind while replying.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该将每个公共属性显示为 DataGridView 上的一列。
如果要更改显示的属性,还需要执行以下操作:
进入 datagridview 的属性,手动添加列并将 DataPropertyName 设置为属性名称。
如果您在代码中创建了 datagridview,则以下内容将创建一列并将其添加到 dgv。
EDIT
这应该会给你一些更接近你想要的东西。 但是,因为它使用匿名类,所以您不能使用 BindingList (据我所知)。 或者,您可以创建一个仅具有您想要显示的属性的 SampleBinding 类,并从正常示例列表中生成这些属性。
EDIT 2
This should display each public property as a column on the DataGridView.
If you want to change which properties are displayed, you need to do the following as well:
and go into the properties of the datagridview, add the columns manually and set the DataPropertyName to the property name.
If you created the datagridview in code, the following will create and add a column to the dgv.
EDIT
This SHOULD give you something closer to what you were wanting. However, because it uses an anonymous class, you can't use BindingList (that I know of). Alternativly, you can create a SampleBinding class that just has the properties you want to be displayed and generate those from the list of normal samples.
EDIT 2
我已经用几种不同的方式处理了这个问题,希望这对您有所帮助。
正如 Justin 提到的,第一个选项是设置 AutoGennerateColumns = false,并从那里手动执行此操作。 如果绑定它,运行时将为 Sample 的所有公共属性创建列。 如果您想删除它们,您可以使用
此解决方案来实现此解决方案有点问题,因为您需要保持更新并显式删除项目。
Justin 的 Edit 2 将属性上的 Browsable 属性设置为 false 的选项很有趣,我以前没有尝试过。
我最终使用的解决方案,我认为效果很好,围绕着一个界面。
我有两个不同的 DataGridView,需要显示相同的数据,但每次显示和隐藏不同的列。 在这种情况下,您将执行以下操作:
然后使用该集合创建示例集合
并绑定到该集合。
如果您想稍后添加列,只需将它们添加到适当的界面即可。
这对我的项目效果很好,让我知道你的想法。
I have handled this a few different ways, hopefully this is helpful.
The first option, as Justin mentioned, is to set AutoGennerateColumns = false, and do it manually from there. If you bind it, the runtime will create columns for all of the public properties of Sample. If you want to remove them, you can do that with
This solution is a bit problematic, as you need to keep it updated, and explicitly remove items.
Justin's Edit 2 option of setting the Browsable attribute to false on the property is interesting, I have not tried that before.
The solution that I have ended up using, and that I think works pretty well revolves around an interface.
I had two different DataGridViews that needed to show the same data, but showing and hiding different colums each time. In this case you would do:
You then create your Sample collection using
and bind to that.
If you want to add columns later, you just add them to the appropriate interface.
This worked well for my project, let me know what you think.
我假设您指的是 WinForms 中的 DataGrid,但这同样适用于大多数可绑定控件。
是的,你可以这样做。 执行此操作的方法分为两步:
BindingList
的实例。I'm assuming you mean DataGrid in WinForms, but the same is applicable to most bindable controls.
Yes you can do this. The way to do this is a 2 step process
BindingList<T>
.