Thursday, February 21, 2008

How to Move Rows in GridView Up and Down

I was developing a project in which I need shuffling of the rows inside gridview. I face alot of hurdles during this because none of the post I saw was providing the complete code for doing it. Therefore as I have finished this task I think that my post might help others who will try to do this.
In this post you will find two codes. First will be the source code and second is the vb code. Both are working in right way.

This Item Template is taking the value from the assigned Database:

<asp:TemplateField HeaderStyle-Font-Bold="true" HeaderText="Date"
ItemTemplate
asp:TextBox ID="cgrid_date" runat="server" EnableViewState="True" Text='<%#DataBinder.Eval(Container.DataItem,"date")%>'
/asp:TextBox
/ItemTemplate
/asp:TemplateField

This ItemTemplate is used for up button and in this I have also assigned Row Number as the value to CommandArgument.

ID="lblUp" runat="serveR" Font-Underline="true" ForeColor="blue" Text="Up" Visible="<%# IIf(CType(Container, GridViewRow).RowIndex = 0, false, true) %>" CommandName="MoveUp" CommandArgument='<%#DirectCast(Container,GridViewRow).RowIndex %>'



From below this is the vb code that I have used.

Imports System.Xml
Imports System.Xml.XPath
Partial Class main_adminNews
Inherits System.Web.UI.Page
Dim myDataSet As New DataSet()
Dim c As New Integer


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If IsPostBack Then

Dim check_point As String = Nothing
myDataSet.ReadXml(Server.MapPath("news1.xml"))
For Each row As GridViewRow In dgUser.Rows
Dim cb As CheckBox = row.FindControl("chkDelete")

If cb IsNot Nothing AndAlso cb.Checked Then
c = 1
If check_point = Nothing Then
check_point = row.RowIndex.ToString
Else
check_point = check_point & "|" & row.RowIndex.ToString
End If


End If
Next
If c = 1 Then
Dim i As Integer = 0
Dim arr() As String = check_point.Split("|")
Dim j As Integer = 0
Do While (i <= arr.Length - 1) myDataSet.Tables(0).Rows(Convert.ToInt32(arr(i - j))).Delete() i = i + 1 j = j + 1 Loop End If End If If c = 1 Then Dim path As String = Server.MapPath("news1.xml") Dim doc As New XmlDocument() Dim xmlfile As String = Server.MapPath("news1.xml") doc.Load(xmlfile) doc.RemoveAll() Dim enc As Encoding Dim writer As New XmlTextWriter(xmlfile, enc) writer.Formatting = Formatting.Indented writer.WriteStartDocument() writer.WriteStartElement("")
writer.Close()

myDataSet.WriteXml(path)

End If

If c = 1 Then
myDataSet.Clear()
readXML()
End If
If Not IsPostBack Then
readXML()
End If

End Sub

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
If Not (Len(txtDate.Text) = 0 And Len(txtDescription.Text) = 0 And Len(txtHeadLine.Text) = 0) Then
writeXML()
End If
End Sub

Private Sub writeXML()
Try
Dim xmlFile As String = Server.MapPath("news1.xml")

Dim xmldoc As New XmlDocument
xmldoc.Load(xmlFile)

Dim node_to_insert As String

node_to_insert = "" & vbNewLine & vbTab & "" & txtDate.Text & "" & vbNewLine & vbTab & "" & txtHeadLine.Text & "" & vbNewLine & vbTab & "" & txtDescription.Text & "" & vbNewLine & Space(3) & ""

Dim docfrag As XmlDocumentFragment = xmldoc.CreateDocumentFragment
docfrag.InnerXml = node_to_insert

xmldoc.DocumentElement.InsertAfter(docfrag, xmldoc.DocumentElement.LastChild)
xmldoc.Save(xmlFile)


txtDate.Text = ""
txtHeadLine.Text = ""
txtDescription.Text = ""

Response.Redirect("adminnews.aspx")
Catch ex As Exception
Response.Write("Error: " & Err.Description)
End Try
End Sub

Private Sub readXML()


myDataSet.ReadXml(Server.MapPath("news1.xml"))
dgUser.DataSource = myDataSet
dgUser.DataBind()


End Sub



Protected Sub dgUser_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgUser.PreRender
''Statement used to hide Up Button on First Row
''I am also using a way to hide the first row in Source Code/Markup
dgUser.Rows(0).Cells(4).Visible = False
''Statement used to hide Down Button on Last Row
dgUser.Rows(myDataSet.Tables(0).Rows.Count - 1).Cells(5).Visible = False
End Sub

Protected Sub dgUser_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles dgUser.RowCommand
If e.CommandName = "MoveUp" Then
Dim i As Integer = (e.CommandArgument)

Dim arr1(myDataSet.Tables(0).Columns.Count) As String
Dim arr2(myDataSet.Tables(0).Columns.Count) As String

Dim l As Integer
For l = 0 To myDataSet.Tables(0).Columns.Count - 1
arr1(l) = myDataSet.Tables(0).Rows(i).Item(l)
arr2(l) = myDataSet.Tables(0).Rows(i - 1).Item(l)
Next

For l = 0 To myDataSet.Tables(0).Columns.Count - 1
myDataSet.Tables(0).Rows(i).Item(l) = arr2(l)
myDataSet.Tables(0).Rows(i - 1).Item(l) = arr1(l)
Next

Dim path As String = Server.MapPath("news1.xml")
myDataSet.WriteXml(path)
Response.Redirect("adminnews.aspx")


ElseIf e.CommandName = "MoveDown" Then
Dim i As Integer = (e.CommandArgument)

Dim arr1(myDataSet.Tables(0).Columns.Count) As String
Dim arr2(myDataSet.Tables(0).Columns.Count) As String

Dim l As Integer
For l = 0 To myDataSet.Tables(0).Columns.Count - 1
arr1(l) = myDataSet.Tables(0).Rows(i).Item(l)
arr2(l) = myDataSet.Tables(0).Rows(i + 1).Item(l)
Next

For l = 0 To myDataSet.Tables(0).Columns.Count - 1
myDataSet.Tables(0).Rows(i).Item(l) = arr2(l)
myDataSet.Tables(0).Rows(i + 1).Item(l) = arr1(l)
Next

Dim path As String = Server.MapPath("news1.xml")
myDataSet.WriteXml(path)
Response.Redirect("adminnews.aspx")

End If
End Sub


End Class


I hope that this might help you.