Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Has anyone been able to bind a coll

Status
Not open for further replies.

Hexonx

Programmer
Jan 10, 2001
102
US
Has anyone been able to bind a collection (array, custom collection, etc.) to a control? I have a custom collection that extends ArrayList and need to bind it to a DataGrid. All to SDK docs examples are for DataSets.

This is my GSWidget class. It contains a custom collection of child GSWidgets. The GSCollection class extends ArrayList.

private class GSWidget
{
public int Value = 0;
public GSCollection Children = new GSCollection();

public GSWidget()
{
Random rnd = new Random();
Value = rnd.Next(1, 1000);
}
}


These are things I've tried:

dataGrid1.DataSource = myWidgets;
dataGrid1.DataMember = "Children.Value";

comboBox1.DataSource = myWidgets;
comboBox1.DisplayMember = "Children.Value";
comboBox1.DataBindings.Add("Text", myWidgets, "Children.Value");


Both of these result in the same ArgumentException: "Can't create child list for field Children".

Any ideas?
 
I have managed to link a list of objects to a DataGrid in the following way.
Let's take the example of a complex numbers class:

class Complex
{
private int real;
private int imag;

//properties associated with the two attributes
public int Real
{
get{return real;}
set{}
}

public int Imag
{
get{return imag;}
set{}
}

public Complex(int real, int imag)
{
this.real = real;
this.imag = imag;
}
}

Now, if you're doing this:

ArrayList ListOfComplexNumbers = new ArrayList(10);
for(int i = 0; i<10;i++)
{
Complex c = new Complex(i,i);
ListOfComplexNumbers.Add(c);
}

//DataGrid dg
dg.DataSource = ListOfComplexNumbers;
dg.DataBind();

you will manage to link the data grid to the collection.
Now, from what I have tried I am 70% sure that the data grid will display only the properties of the Complex class. If you comment the Real or the Imag property, they won't be data bound to the grid.

For a DropDownList it works the same way, except for the fact that you can set the value and text fields:

dropdownlist.DataSource = ListOfComplexNumbers;
dropdownlist.DataTextField = &quot;Real&quot;;
dropdownlist.DataValueField = &quot;Imag&quot;;

I hope this helps.
 
I found in a book that controls will only bind to properties and not fields. This was the whole problem with my example. I even converted the Children collection from a public field to a public property and the DataGrid displayed the full hierarchy. If the property is read-write or read-only, controls, such as the DataGrid, will exhibit edit behavior accordingly.

Now I want to work on setting the columns in a custom order (override the DataGrid's default column ordering) and change the display template. I'll post my findings.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top