Skip to main content

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.

Comments

Popular posts from this blog

The sign-in method you're trying to use isn't allowed. For more info, contact your network administrator

Above problem occur when you have not added a group (of which User try to login)  in your local policy login. To resolve the issue, follow the steps,
Log in as the administrator on the server, then start the Group Policy Management Editor by running the  gpmc.msc command from PowerShell or the Command Line window. 1. In the Group Policy Management window on the left hand side, select Group Policy Management. 2. Click to expand the  Forest tree node.
3. Click Domains.
4. Select your domain name.
5. Click Group Policy Objects. 6. In the right-hand window, double-click Default Domain Controllers Policy.
7. Right-click Default Domain Controllers Policy and Select Edit.
8. In the Group Policy Management Editor window, click Default Domain Controllers Policy.
9. Click Computer Configuration, and then click Policies.
10. Click Windows Settings, and then click Security Settings. 11. Click Local Policies and then click User Rights Assignments.
12. In the right pane, click Allow log on locally. 12. Cli…

ORA-38104: Columns referenced in the ON Clause cannot be updated

In my case, this error pertains to Oracle Merge query. After 30 minutes of time wastage and a lot of hunting on different forums, I come to know that where and why was my merge query throwing exception.

I was giving multiple criteria in ON clause of merge query, and was also trying to update a column which I have mentioned in ON clause. Once I remove that column from update clause of merge query, it executed successfully.

I hope that this will also works for you.

In case if this post help you, then please comment on it.

Thanks.

Cannot use the special principal 'sa'. (Microsoft SQL Server, Error: 15405)

Yesterday, I start SQL Server 2005 and try to connect with sa user but was enable to connect with the 'sa' user and got this error:

Cannot use the special principal 'sa' (Microsoft SQL Server, Error: 15405)After doing some google, I found out the solution which I am sharing so that others can get all the possible solutions from this article.
The titled message mean that your sa user is disabled. To enable it, you have to follow below mentioned steps:1) you have to change the authentication mode: go to Management Studio and open Object Explorer. Connect to your server and right-click on the server name, then select Properties. Go to Security tab and under Server Authentication select "SQL Server and Windows Authentication Mode".2) Also, after you follow the directions above, you will also need to explicitly enable the sa account as well. Go to Security->Logins. Right-click on the "sa" account, select "Properties". In the list on the lef…