开发者

C# Tutorial - Binding a DataGridView to a Collection which contains an object

So I have a datagridview.

Normally I can create a bindinglist of my object and it will display all the data and colums correctly

E.g

BindingList<Customer> CustomerList = new BindingList<Customer>();
myDataGridView.DataSource = CustomerList;

And it will display a column for each property in Customer which has a getter. It will also display a row for each object.

Perfect result.

Now in my current scenario, I have these objects:

public class Reservedele
{
    public int Rnr { get; private set;}
    public string Navn { get; private set;}
    public double Pris { get; private set;}
    public string Type { get; private set;}

    public Reservedele(int rnr, string navn, double pris, string type)
    {
        Rnr = rnr;
        Navn = navn;
        Pris = pris;
        Type = type;
    }
}
public class IndkøbsKurvReservedele
{
    public Reservedele Reservedel  { get; private set;}
    public int Antal { get; private set; }
    public double Pris { get; private set; }

    public IndkøbsKurvReservedele(Reservedele reservedel, int antal, dou开发者_运维问答ble pris)
    {
        Reservedel = reservedel;
        Antal = antal;
        Pris = pris;
    }
}

So if I do this:

BindingList<IndkøbsKurvReservedele> ReserveDeleListeIndKøbsKurv = new BindingList<IndkøbsKurvReservedele>();
myDataGridView.DataSource = ReserveDeleListeIndKøbsKurv;

It won't work :( The error is that the datagridview shows:

  • The object itself
  • Antal
  • Pris

Example:

C# Tutorial - Binding a DataGridView to a Collection which contains an object

How can I instruct the datagridview that I want it to have the properties from Reservedel followed by the properties in IndkøbsKurvReservedele as headers??

For reference, Reservedel means "spare parts" and IndkøbsKurvReservedel means something like "ShoppingBasketOfSpareParts". Antal means "amount" as in how many there is. Sorry for the foreign variable naming.

EDIT: I have another question: How can I make the Rnr property not to show up as a column?


Technically you can't, the datasource doesn't bind any nested properties.

But you can always workaround by exposing the properties of your child object in the parent.


You also can override the ToString() method of the Reservedele class to display whatever you want (by default the DataGridView will call the ToString() method to get the cell content).


You could always use LINQ and do something like:

var bindMe =
            from r in ReserveDeleListeIndKøbsKurv
            select new
            {
                navn = r.Reservedel.Navn,
        pris = r.Reservedel.Pris,
        type = r.Reservedel.Type,
        antal = r.Antal,
        anotherPris = r.Pris
            };

myDataGridView.DataSource = bindMe;


You can't unless you inherit that class or you wrap all this data into some sort of dataset. I don't know the relations of your classes or how they fit together. With the current posting you gave us it isn't possible.

Also how dare you create variables as foreign words! :) just kidding


This is my solution:

public class IndkøbsKurvReservedele
{
    //public Reservedele Reservedel  { get; private set;}
    private int Rnr;
    public string Navn { get; private set; }
    public double Pris { get; private set; }
    public string Type { get; private set; }
    public int Antal { get; private set; }
    public double SPris { get; private set; }

    public IndkøbsKurvReservedele(Reservedele reservedel, int antal, double sPris)
    {
        Rnr = reservedel.Rnr;
        Navn = reservedel.Navn;
        Pris = reservedel.Pris;
        Type = reservedel.Type;
        Antal = antal;
        SPris = sPris;
    }
    public int HenrReservedelsNummer()
    {
        return Rnr;
    }
}

If anyone creates a better. I will accept theirs instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜