comboBox displays bound data out of order

D

Dica

i've got a dataSet that looks like the following:

<tblTasks>

<id>0</id>

<summary>-- Select A Task--</summary>

</tblTasks>

<tblTasks>

<id>713</id>

<summary>research</summary>

</tblTasks>

<tblTasks>

<id>717</id>

<summary>Other - time estimation</summary>

</tblTasks>



i've bound the dataSet to my comboBox as follows:

cboTasks.DataSource = null;

cboTasks.Items.Clear();

cboTasks.DataSource = oDataSet;

cboTasks.DisplayMember = "tblTasks.summary";

cboTasks.ValueMember = "tblTasks.id";



but the "-- Select Task --" option is showin up last. any idea why or how to
fix?
 
L

Lars Behrmann

Hi Dica,

you can set the DefaultView of your DataTable
as DataSource and set the Sort property of the
DefaultView to ascending. You should also set
your id column as primary key. Otherwise it could
be that the sorting won't work.

.....DataSource = oDataSet.Tables["tblTasks"].DefaultView.Sort =
"ascending";

Cheers
Lars Behrmann

_________________
Nothing is impossible. UML is the key for all your problems.
AODL - Make your .net apps OpenOffice ready
http://aodl.sourceforge.net/
 
D

Dica

Lars Behrmann said:
Hi Dica,

you can set the DefaultView of your DataTable
as DataSource and set the Sort property of the
DefaultView to ascending. You should also set
your id column as primary key. Otherwise it could
be that the sorting won't work.

....DataSource = oDataSet.Tables["tblTasks"].DefaultView.Sort =
"ascending";

thanks lars.
i've revised the code as per your instrucitons:
cboTasks.DataSource = oDataSet.Tables["tblTasks"].DefaultView.Sort = "id";


cboTasks.DisplayMember = "summary";

cboTasks.ValueMember = "id";



but i'm getting an error:
Complex DataBinding accepts as a data source either an IList or an
IListSource

the other option i've found is to use a select statement againts the dataSet
table:

DataRow[] drarray;

drarray = oDataSet.Tables["tblTasks"].Select(null, "id",
System.Data.DataViewRowState.CurrentRows);

for (int i=0; i < drarray.Length; i++)

{

cboTasks.Items.Add(drarray["summary"].ToString());

}



this option seems to work fine, but i can't find any way to add the
valueMember (the ID column from my dataSet) to the comboBox this way.



any other ideas?



tks
 

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