DataGrid not binding in correct order from DataTable

C

chotch

Hello,
I am having a spot of trouble with binding a DataTable to a DataGrid.
My problem originates after I have created the DataTable and DataGrid.
I present the grid to the user with the options to move rows up or
down. When a user selects one of these, I get the DataTable from the
session, rearrange the rows, and attempt to rebind the DataGrid to the
DataTable. This is when the problem occurs, and the DataGrid messes up
the order of the rows, even when the DataTable has the correct Data.
Has anyone encountered a problem like this? All my other DataGrid code
works fine.

Thanks in advance to anyone who takes time to respond! :)

Here is a little snippit:

int toItemIndex = to.ItemIndex;
int fromItemIndex = from.ItemIndex;

DataRow drFrom = conditionsTable.NewRow();
DataRow drTo = conditionsTable.NewRow();
drFrom["fields_id"] = from.Cells[9].Text;
drFrom["label"] = from.Cells[2].Text;
drTo["fields_id"] = to.Cells[9].Text;
drTo["label"] = to.Cells[2].Text;

//rearrange the order of the rows
conditionsTable.Rows.RemoveAt(toItemIndex);
conditionsTable.Rows.InsertAt(drFrom,toItemIndex);
conditionsTable.Rows.RemoveAt(fromItemIndex);
conditionsTable.Rows.InsertAt(drTo,fromItemIndex);
where.DataSource = conditionsTable.DefaultView;
where.DataBind();
 
R

R. Thomas, aka Xtreme.Net

When u are binding the new table i.e the table with the re-arranged rows to
the dg, are u sure the data is as u want it to be? or is it still in the
original way?
R. Thomas
 
C

chotchkie

Yes when I debug through, the conditions table is in the exact order as
I want it. It also should be said that the datagrid where is bound to
conditionsTable previously, before the post back.

Also before the all the work is done above, the datagrid matches the
datatable (as it should), in order and in content
 
R

R. Thomas, aka Xtreme.Net

Did u clear the datagrid before re-binding?
cause i tried, and it works fine for me... my dg follows my table everytime...
R. Thomas
 
C

chotchkie

Hmmm, well I found the problem, though I am not sure how to get around
it and am open to any ideas (other than the massively expensive
creating an entirely new datatable and destroying the old one)

When i step through the debugger, each DataRow in the DataTable.Rows
has a private variable called rowId. When rows are created, rowId is
the next in the list. When I perform the InsertAt, it places the row
in the correct position in the DataTable, but the rowId is still the
highest. When I bind to the DataGrid, it seems to be sorting by rowId,
no matter the order of the rows in the DataTable. There is no sorting
or filtering strings in the DataGrid.

Any Ideas?
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top