绑定到 BindingList- 选择绑定什么?

发布于 2024-07-29 13:13:39 字数 235 浏览 10 评论 0原文

假设我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

输什么也不输骨气 2024-08-05 13:13:39
BindingList<Sample> samples = new BindingList<Sample>();
DataGridView dgv = new DataGridView();
dgv.DataSource = samples;

这应该将每个公共属性显示为 DataGridView 上的一列。
如果要更改显示的属性,还需要执行以下操作:

dgv.AutoGenerateColumns = false;

进入 datagridview 的属性,手动添加列并将 DataPropertyName 设置为属性名称。

如果您在代码中创建了 datagridview,则以下内容将创建一列并将其添加到 dgv。

DataGridViewColumn dgvc = new DataGridViewColumn();
dgvc.Name = "PropertyA";
dgvc.HeaderText = "Property A";
dgvc.DataPropertyName = "PropertyA";
dgv.Columns.Add(dgvc);


EDIT

这应该会给你一些更接近你想要的东西。 但是,因为它使用匿名类,所以您不能使用 BindingList (据我所知)。 或者,您可以创建一个仅具有您想要显示的属性的 SampleBinding 类,并从正常示例列表中生成这些属性。

public class Sample
{
    public int PropertyA {get;set;}
    public bool PropertyB {get;set;}
    public string PropertyC {get;set;}
    public double PropertyD {get;set;}
}

List<Sample> samples = new List<Samples>(GetSamples());
var sampleBinding = from sample in samples
                    select new
                    {
                        PropertyA = sample.PropertyA,
                        PropertyC = sample.PropertyC
                    };

BindingList bl = new BindingList();
bl.DataSource = sampleBinding;
dgv.DataSource = bl;


EDIT 2

public class Sample
{
    [Browsable(false)]
    public int PropertyA {get;set;}
    public bool PropertyB {get;set;}
    public string PropertyC {get;set;}
    [Browsable(false)]
    public double PropertyD {get;set;}
}
BindingList<Sample> samples = new BindingList<Sample>();
DataGridView dgv = new DataGridView();
dgv.DataSource = samples;

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:

dgv.AutoGenerateColumns = false;

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.

DataGridViewColumn dgvc = new DataGridViewColumn();
dgvc.Name = "PropertyA";
dgvc.HeaderText = "Property A";
dgvc.DataPropertyName = "PropertyA";
dgv.Columns.Add(dgvc);


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.

public class Sample
{
    public int PropertyA {get;set;}
    public bool PropertyB {get;set;}
    public string PropertyC {get;set;}
    public double PropertyD {get;set;}
}

List<Sample> samples = new List<Samples>(GetSamples());
var sampleBinding = from sample in samples
                    select new
                    {
                        PropertyA = sample.PropertyA,
                        PropertyC = sample.PropertyC
                    };

BindingList bl = new BindingList();
bl.DataSource = sampleBinding;
dgv.DataSource = bl;


EDIT 2

public class Sample
{
    [Browsable(false)]
    public int PropertyA {get;set;}
    public bool PropertyB {get;set;}
    public string PropertyC {get;set;}
    [Browsable(false)]
    public double PropertyD {get;set;}
}
や莫失莫忘 2024-08-05 13:13:39

我已经用几种不同的方式处理了这个问题,希望这对您有所帮助。

正如 Justin 提到的,第一个选项是设置 AutoGennerateColumns = false,并从那里手动执行此操作。 如果绑定它,运行时将为 Sample 的所有公共属性创建列。 如果您想删除它们,您可以使用

DataGridView.Columns["SomePropertyOfSample"].Remove();

此解决方案来实现此解决方案有点问题,因为您需要保持更新并显式删除项目。

Justin 的 Edit 2 将属性上的 Browsable 属性设置为 false 的选项很有趣,我以前没有尝试过。

我最终使用的解决方案,我认为效果很好,围绕着一个界面。

我有两个不同的 DataGridView,需要显示相同的数据,但每次显示和隐藏不同的列。 在这种情况下,您将执行以下操作:

public interface ISimpleSample
{
  string Name {get;}
  int ID {get;}
}

public interface IAdvancedSample
{
  string Name {get; set;}
  int ID {get; set;}
  string Make {get; set;}
  string Model {get; set;}
}

public class Sample : ISimpleSample, IAdvancedSample
{
  //Implementation skipped
}

然后使用该集合创建示例集合

BindingList<ISimpleSample> = new BindingList<ISimpleSample>();

并绑定到该集合。

如果您想稍后添加列,只需将它们添加到适当的界面即可。

这对我的项目效果很好,让我知道你的想法。

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

DataGridView.Columns["SomePropertyOfSample"].Remove();

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:

public interface ISimpleSample
{
  string Name {get;}
  int ID {get;}
}

public interface IAdvancedSample
{
  string Name {get; set;}
  int ID {get; set;}
  string Make {get; set;}
  string Model {get; set;}
}

public class Sample : ISimpleSample, IAdvancedSample
{
  //Implementation skipped
}

You then create your Sample collection using

BindingList<ISimpleSample> = new BindingList<ISimpleSample>();

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.

何必那么矫情 2024-08-05 13:13:39

我假设您指的是 WinForms 中的 DataGrid,但这同样适用于大多数可绑定控件。

是的,你可以这样做。 执行此操作的方法分为两步:

  • 将 DataSource 成员设置为 BindingList 的实例。
  • 将 DataMember 属性设置为要绑定的属性的字符串名称。

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

  • Set the DataSource member to be the instance of the BindingList<T>.
  • Set the DataMember property to be the string name of the property you want bound.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文