tags:

views:

376

answers:

5

Hi.

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, double 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: alt text

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?

A: 

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

JonH
Can't you manually instruct it either?
CasperT
+1  A: 

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.

K2so
Could you show an example?
CasperT
A: 

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.

CasperT
+1  A: 

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).

MF
Really? ToString()? I had no idea.But how can it separate the string into Coloumns? What does it use as a separator?
CasperT
In the exposed problem, you bind columns with object properties. This properties can be native types (string, int, double, etc) or objects. If the property is an object, the ToString() method is called to fill the cell text. So if you override this method to return ¨It Works¨, the cell content binded to the object property will look like ¨It Works¨.So you doesn´t separate strings into columns, that is already done in design time...Do you get the idea? I can send you an example if you want.
MF
An example please :)
CasperT
Ok. imagine this two classes:public class Parent{ public string Name{get;set;} public Child Child{get;set;}}public class Child{ public string Name{get;set;} public override string ToString() { return Name.ToString(); }}Now we bind a list of Parent Objects to a DataGridView:void F_Load(object sender, EventArgs e){ List<Parent> p = new List<Parent>(); p.Add(new Parent(){Name="Parent1",Child=new Child(){Name="Child1"}});//add more childs dtGrid.DataSource= p;}You will see two columns "Name" and "Child", and the content of the child column will be Child#
MF
+1  A: 

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;

Nick
But this won't have the same effect as an binding list, will it? datagrid has to update itself when changes happens in the list.
CasperT