Addign a row to a dataset

G

Guest

VS2003 C#
Hi,

My dataset is ordered. When I add another row to it by creating a new row
then using AddNew(), & then persisting it to the underlying table,
thencaccepting changes to the dataset, the new row is appended to the
datasets datatable, but not ordered.

How can I add it to the table but have it placed in the order it should be
in (particular row Ascending, for example.)?

Currently I need to end & reload the application for the dataset to be
rebuilt in the correct order.

Any ideas would be most appreciated.

ant
 
N

Norman Yuan

Firstly, if you really want to add a new DataRow into a DataTable in
particular order, you can use DataTable.Rows.InsertAt(), instead of
DataTable.Rows.Add();

Secondly, AcceptChanges() has nothing to do with order here;

Thirdly, there is almost no need to bother DataRow's order in DataTable. You
can use DataView (created on top of DataTable, or the DataTable's default
DataView) to sort the DataTable whatever way you want to.
 
G

Guest

Hello Norman,

Thank you for your tips.

I am in fact working with a dataview, however this doesn't seem to fix the
problem.


I use the code below to create a fresh data view each time a user triggers
the textcanged event of the text box as below:


string filter = "companyName LIKE '" + txtsearch.Text + "%'";
DataView dvFind = new DataView(dsMain.contact);

dvFind.RowFilter = filter;
lstFind.DataSource = dvFind;
lstFind.DisplayMember = "companyName";

This is used to populate a list box with company names derived from the
filter, however, the indexes seem to be offset with each new insert.

why is this so?

Thanks for any help

Ant
 
J

Jerry H.

Look into using the "Sort" method of your View object, like so:

dvFind.RowFilter = filter;
dvFind.Sort="State, ZipCode DESC"
lstFind.DataSource = dvFind;
lstFind.DisplayMember = "companyName";

Of course, replace State and Zipcode with your real column names.

HTH
 
C

Cor Ligthert [MVP]

Ant,

As the others wrote already, There is in fact no reason to get a dataset in
a real sequence. However there can be one reason that is if you serialize
your dataset and want to access it in a sequential way.

Than there is now the new overloaded DataView.ToTable method.

http://msdn2.microsoft.com/en-us/library/a8ycds2f.aspx

I hope this helps,

Cor
 
G

Guest

Hi Cor,

Thanks for the input.

Are you suggesting the best way to do this would be to bind a Dataview to
the controls instead of a Dataset?

Ccurrently I have a typed dataset bound to the testbox controls. I then use
a dataview to filter the 'user choice'.

How would this be best done?

Thanks for any ideas
Ant
 
C

Cor Ligthert [MVP]

Ant,

A dataview is a part of a datatable which are part of datasets.

Mostly it is better to use a dataview (or defaultview as it is intern in the
datatable) than direct a datatable.

A dataset you can only bind to a DataGrid for the rest the standard
Microsoft controls accept only datatables or dataviews (or other
collection/lists)

Cor
 
G

Guest

Thanks Cor,

Upon your recommendation I decided to set the binding context as a dataview
& filter that. Everything works fine now. thanks very much for that insight.

Cheers
Ant
 

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