I have a gridview populated using a datatable. Within my gridview I have 2 buttons "Up" and "Down". The "Up" button takes the selected row and moves it up one row. The "Down" button takes the selected row down a row. The way i'm doing this is updating a column called "Order" and having my grid sorted by Page and Order columns. If going up I swap order values within the grid with the row above the selected row and the opposite for down. I have to be able to insert, delete and move rows up and down. I'm trying to keep track of the grid rowindex and update that cell.
Ex.
Row# Page Order Text
1 1 10 Test1
2 1 20 Test2
3 1 30 Test3
Say I move row# 2 up now It should look like this
1 1 10 Test2
2 1 20 Test1
3 1 30 Test3
My problem is if I try and add a new row between rows#1-2 I should have
1 1 10 Test2
2 1 20 Test4
3 1 30 Test1
4 1 40 Test3
Should I be updating my datatable? I'm updating my grid. Here is my RowChanging event
Thank you for any light you can shed on this.
Ex.
Row# Page Order Text
1 1 10 Test1
2 1 20 Test2
3 1 30 Test3
Say I move row# 2 up now It should look like this
1 1 10 Test2
2 1 20 Test1
3 1 30 Test3
My problem is if I try and add a new row between rows#1-2 I should have
1 1 10 Test2
2 1 20 Test4
3 1 30 Test1
4 1 40 Test3
Should I be updating my datatable? I'm updating my grid. Here is my RowChanging event
private void gv_RowsChanging(object sender, GridViewCollectionChangingEventArgs e) |
{ |
if (e.Action == Telerik.WinControls.Data.NotifyCollectionChangedAction.Add) |
{ |
RowSetup = e.NewItems[0] as GridViewDataRowInfo; |
RowSetup.Cells["ID"].Value = gv.Rows[e.NewStartingIndex - 1].Cells["ID"].Value; |
RowSetup.Cells["PAGE"].Value = gv.Rows[e.NewStartingIndex - 1].Cells["PAGE"].Value; |
RowSetup.Cells["ORDER"].Value = Convert.ToInt32(gv.Rows[e.NewStartingIndex - 1].Cells["ORDER"].Value) + Convert.ToDecimal(".5"); |
foreach (GridViewRowInfo dri in gv.Rows) |
{ |
if (dri.Cells["PAGE"].Value.ToString() == RowSetup.Cells["PAGE"].Value.ToString()) |
{ |
if (Convert.ToDecimal(dri.Cells["ORDER"].Value) > Convert.ToDecimal(RowSetup.Cells["ORDER"].Value)) |
{ |
dri.Cells["ORDER"].Value = Convert.ToInt32(dri.Cells["ORDER"].Value) + 1; |
} |
} |
} |
RowSetup.Cells["ORDER"].Value = Convert.ToDecimal(gv.Rows[e.NewStartingIndex].Cells["ORDER"].Value) + Convert.ToDecimal(".5"); |
} |
} |
Thank you for any light you can shed on this.