Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding to BindingList<T> - choose what to bind?

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.

like image 959
sarsnake Avatar asked Jul 24 '09 20:07

sarsnake


People also ask

What is a bind list?

BindingList is a generic list type that has additional binding support. While you can bind to a generic list, BindingList provides additional control over list items, i.e. if they can be edited, removed or added. BindingList also surfaces events that notify when the list has been changed.

How to bind object in c#?

When an object is assigned to an object variable of the specific type, then the C# compiler performs the binding with the help of . NET Framework. C# performs two different types of bindings which are: Early Binding or Static Binding.

What is the difference between ObservableCollection and BindingList?

The practical difference is that BindingList is for WinForms, and ObservableCollection is for WPF. From a WPF perspective, BindingList isnt properly supported, and you would never really use it in a WPF project unless you really had to.


1 Answers

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;}
}
like image 61
Justin Drury Avatar answered Sep 21 '22 19:09

Justin Drury