Move a row in a datalist

T

tshad

Is there a way to move a row in a Datalist up or down without having to
re-read the data?

I have a datalist which has embedded Datagrids in it. I want to allow the
user to move a row up or down by just pushing a button and have the page
post back with the row moved without actually re-reading the data from the
database?

Thanks,

Tom.
 
J

John Saunders

tshad said:
Is there a way to move a row in a Datalist up or down without having to
re-read the data?

I have a datalist which has embedded Datagrids in it. I want to allow the
user to move a row up or down by just pushing a button and have the page
post back with the row moved without actually re-reading the data from the
database?

If the DataList is data bound, then the order of the rows is the order of
the data. Somehow, you'll have to change the order of the data when the user
drags the row.

John Saunders
 
T

tshad

John Saunders said:
If the DataList is data bound, then the order of the rows is the order of
the data. Somehow, you'll have to change the order of the data when the
user drags the row.

The data is databound and if they make a change, then I will change the
data, but I don't want to have to reread the data to re-fill the datalist,
which would entail reading the data to fill the grids also.

Tom.
 
E

Eliyahu Goldin

You can achieve it with heavy javascripting. And you will need to find a way
of transferring new order back to server. Something like merging item values
into one string, passing it in a hidden <input>, parsing on the server and
setting new order in the database. Quite a challenging task.

Eliyahu
 
J

John Saunders

tshad said:
The data is databound and if they make a change, then I will change the
data, but I don't want to have to reread the data to re-fill the datalist,
which would entail reading the data to fill the grids also.

Ok, what if you keep the data in a DataSet in Session state. Databind it to
the DataList through a DataView. Have the DataView sorted by a SortOrder
column. Have the row move change the SortOrder field of the dropped row and
those following its new position. When you DataBind again, the moved row
should be in its new position.

John Saunders
 
T

tshad

John Saunders said:
Ok, what if you keep the data in a DataSet in Session state. Databind it
to the DataList through a DataView. Have the DataView sorted by a
SortOrder column. Have the row move change the SortOrder field of the
dropped row and those following its new position. When you DataBind again,
the moved row should be in its new position.

That sounds like it might work.

How do you keep the DataSet in a Session State?

The sort order would be kept by letter or some number which I would change
(swap) with the the row above or below the row being moved. I would then
make the change to Sql Server using an update command.

How would I make the same change to the dataset?

What about my grids? Would I lose the Grid data for each grid that is
connected to the rows?

Thanks,

Tom.
 
J

John Saunders

tshad said:
That sounds like it might work.

How do you keep the DataSet in a Session State?

You can store just about anything in Session state:

Session("data") = myDataSet
' later, or on another page:
Dim myDataSet = DirectCast(Session("data"), DataSet)

You have to check to make sure that Session("data") is not Nothing, and do
whatever you need to do to create and fill the DataSet in that case. It's
best to do this within a property or method so that the code to do it is all
in one place. Here's an example:

Private _dataSet As DataSet

Protected Property Data() As DataSet
Get
Dim table As DataTable
If Session("data") Is Nothing Then
' Do whatever you have to in order to get the data
' back into Session state
table = New DataTable("Table")
table.Columns.Add("column1", GetType(String))
table.Columns.Add("column2", GetType(String))
_dataSet = New DataSet("dataSet")
_dataSet.Tables.Add(table)
Session("data") = _dataSet
Else
_dataSet = DirectCast(Session("data"), DataSet)
End If
End Get
Set(ByVal Value As DataSet)
_dataSet = Value
Session("data") = _dataSet
End Set
End Property

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
BindGrid()
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim dr As DataRow = Data.Tables(0).NewRow()
dr("column1") = TextBox1.Text.Trim()
dr("column2") = TextBox2.Text.Trim()
Data.Tables(0).Rows.Add(dr)
'
BindGrid()
End Sub

' Separate sub is useful in case this gets fancier,
' with a DataView and sorting, for instance
Private Sub BindGrid()
Dim dv As New DataView
dv.Table = Data.Tables(0)
dv.Sort = "SortOrder"
DataGrid1.DataSource = dv
DataGrid1.DataBind()
End Sub
The sort order would be kept by letter or some number which I would change
(swap) with the the row above or below the row being moved. I would then
make the change to Sql Server using an update command.

How would I make the same change to the dataset?

' Find the appropriate row, however you do it, then:
dr("SortOrder") = newSortOrder ' Done
What about my grids? Would I lose the Grid data for each grid that is
connected to the rows?

No. You would call DataBind again, or better, call something like the
BindGrid in my example above.

John Saunders
 
G

Guest

Hi,

In addition to adding the dataset, which contains the datatable to the
Session, you can add a datacolumn called "Order" and fill it with the number
in which the client selects the order of the datarows. And you can create a
dataview, sort it with the "Order" column and bind it to datagrid.

Hope it helps.

Prakash.C
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,253
Latest member
BlytheFant

Latest Threads

Top