combobox databinding

D

Dan

I'm binding a combobox to a table of a typed dataset; in design mode, I set
the datasource to the dataset table and its displaymember and valuemember as
required. The binding works fine, and if I add or edit a record in the
underlying dataset table this is reflected by the combobox items list.

My question is: *how can I keep the items in the combo sorted* by its
display member after a new record has been added or an existing one has been
edited? Now, they are simply appended to the table end and consequently they
appear last in the combo. The original sorting came from the data adapter
which fille the dataset (its select command has an order by clause), but
when working disconnected (as this is the case when adding/editing combobox
records) this does not matter...

Thanks guys!
 
W

William Ryan

Have you set its Sorted Property to true? If not, then set it, and you can
always use its Refresh method afterward.

HTH,

Bill
 
K

Kevin Yu [MSFT]

Hi Dan,

You can achieve this by creating a DataView from the table. First, you
create a DataView which is sorted on the column you need to sort. In the
example, we sort on "City" field.

DataView dv = new DataView(ds.Tables[0], "", "City",
DataViewRowState.CurrentRows);

Then you can bind the DataView to the ComboBox, and set the display and
value member. When adding a row to the DataSet, the DataView will be sorted
automatically.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| From: "Dan" <[email protected]>
| Subject: combobox databinding
| Date: Thu, 6 Nov 2003 12:00:34 +0100
| Lines: 16
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.adonet
| NNTP-Posting-Host: ppp-217-133-156-212.cust-adsl.tiscali.it
217.133.156.212
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:65622
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| I'm binding a combobox to a table of a typed dataset; in design mode, I
set
| the datasource to the dataset table and its displaymember and valuemember
as
| required. The binding works fine, and if I add or edit a record in the
| underlying dataset table this is reflected by the combobox items list.
|
| My question is: *how can I keep the items in the combo sorted* by its
| display member after a new record has been added or an existing one has
been
| edited? Now, they are simply appended to the table end and consequently
they
| appear last in the combo. The original sorting came from the data adapter
| which fille the dataset (its select command has an order by clause), but
| when working disconnected (as this is the case when adding/editing
combobox
| records) this does not matter...
|
| Thanks guys!
|
|
|
 

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