Sunday 13 September 2015

moving rows of DataTable up and down in asp.net using C#


moving record of datatable in asp.net




Description:-


In this example we explain that how to move rows up and down in datatable in asp.net. or Moving row of a DataTable up and down. Or interchange the position of DataRow in DataTable or change the index or position of the datarow in datatable in asp.net.

Sometime You will need to add an Order column, and assign the DataGrid with a DataView sorted by the Order column. In order to switch the position of rows, you will need to set the Order value of each row.

That was the real problem that I was faced before sometime and then now I wil solved it.my problem was that I want to arrane thje datarow in datatable. In my datatable's one coloumn is that "colorarea". And if colorarea's value in datarow is "cell" then its position remain same in datatable but colorarea's value is "row" then it's datarow position are moved to the last position of the Datatable rows.

So how to handle that's type of situation and so far we will demonstrate to move or change the position of the datarow in datatable in asp.net using c#.


Code:-

DataRow selectedRow = table.Rows[idx];
DataRow newRow = table.NewRow();
newRow.ItemArray = selectedRow.ItemArray; // copy data
table.Rows.Remove(selectedRow);
table.Rows.InsertAt(newRow, idx +1/-1);

 

2 comments: