datagridview view problem

P

placentiusz

class c1 {
private int field1;
private c2 field2;
public int Field1
{
get { return this.field1; }
set { this.field1 = value; }
}

public c2 Field2
{
get { return this.field2; }
set { this.field2 = value; }
}
public c1()
{
this.field1=1;
this.field2=null;
}
}

class c2 {
private int field3;
private int field4;
public int Field3
{
get { return this.field3; }
set { this.field3 = value; }
}
public int Field4
{
get { return this.field4; }
set { this.field4 = value; }
}
public c2()
{
this.field3=3;
this.field4=4;
}

}


i create objects:

c1 example = new c1();
c2 example2 = new c2();
example.Field2 = example2;
Arraylist newlist = new Arraylist();
newlist.Add(example);

object newlist is datasource for datagridview :

datagridview1.datasource=newlist;

i want to get table:
 
K

kndg

Hi placentiusz,

Hmm...I would modify your c1 class as below,

class c1 // ...good naming convention suggest that a class name should
starts with upper-case letter
{
private int field1;
private c2 field2;

public int Field1
{
get { return field1; }
set { field1 = value; }
}

public int Field3
{
get { return c2.Field3; }
set { c2.Field3 = value; }
}

public int Field4
{
get { return c2.Field4; }
set { c2.Field4 = value; }
}

public c1(c2 myC2)
{
if (myC2 == null) throw new ArgumentNullException();
field1 = 1;
field2 = myC2;
}
}

Then,

c2 example2 = new c2();
c1 example = new c1(example2);

// since you are using DataGridView which is only available on framework
// version 2.0 or later, you should favor generic list than ArrayList.
List<c1> newlist = new List<c1>();
newlist.Add(example);

datagridview1.DataSource = newlist;

Regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top