C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » DataGridView »

Jump to nth row or move up/down row on single click


Posted Date: 22 Oct 2008    Resource Type: Code Snippets    Category: DataGridView
Author: Vinod ChattergeeMember Level: Silver    
Rating: 1 out of 5Points: 10



To Move row up/down:

1.Place the below template column inside your column tag in Datagrid


<asp:TemplateColumn>
<ItemTemplate>
<asp:ImageButton runat="server" ToolTip="Click to move this row up" ImageUrl="~/Images/Up.gif" ID="btnUp" CommandName="MoveUp" />
</ItemTemplate>
</asp:TemplateColumn>




2.Place Up.gif(or what ever the image you wants) inside the image folder of the root of your website.

3. Paste the below code inside the item command of your grid

Private Sub YourDataGrid_ItemCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs)
Dim rowID As Int32 = DirectCast(dgMainMenu.DataKeys(e.Item.ItemIndex), Int32)

Dim rowIndex As Int32 = 0
Dim rowToBeMoved As DataRow = Maindt.NewRow()
For i As Int16 = 0 To CShort(Maindt.Rows.Count - 1)
'create a copy of the row to be moved
If Maindt.Rows(i)("MNU_MENU_ID").Equals(rowID) Then
rowIndex = i
rowToBeMoved("MNU_MENU_ID") = Maindt.Rows(i)("MNU_MENU_ID")
rowToBeMoved("MNU_ITEM_NAME") = Maindt.Rows(i)("MNU_ITEM_NAME")
rowToBeMoved("MNU_ITEM_URL") = Maindt.Rows(i)("MNU_ITEM_URL")
Exit For
End If
Next

Select Case e.CommandName
Case "MoveUp"
If rowIndex > 0 Then
'delete the selected row
Maindt.Rows(rowIndex).Delete()
Maindt.AcceptChanges()
'add the rowToBeMoved
Maindt.Rows.InsertAt(rowToBeMoved, rowIndex - 1)
Maindt.AcceptChanges()

dgMainMenu.SelectedIndex = rowIndex - 1
End If
Exit Select
Case "MoveDown"
If rowIndex < Maindt.Rows.Count - 1 Then
Maindt.Rows(rowIndex).Delete()
Maindt.AcceptChanges()
Maindt.Rows.InsertAt(rowToBeMoved, rowIndex + 1)
Maindt.AcceptChanges()
dgMainMenu.SelectedIndex = rowIndex + 1
End If
Exit Select
Case "Jump"
Try
Dim txtJumpTemp As TextBox = CType(e.Item.FindControl("txtJump"), TextBox)
Dim jumprowIndex As Integer = CInt(txtJumpTemp.Text)
If rowIndex >= 0 And rowIndex < Maindt.Rows.Count Then
'delete the selected row
Maindt.Rows(rowIndex).Delete()
Maindt.AcceptChanges()
'add the rowToBeMoved
Maindt.Rows.InsertAt(rowToBeMoved, jumprowIndex - 1)
Maindt.AcceptChanges()

dgMainMenu.SelectedIndex = jumprowIndex - 1
End If
Catch ex As Exception
'ShowMessage("Specify a Row No. - Should be an Integer")
End Try

Exit Select
End Select

Session("MainOrder") = Maindt
End Sub



.. To be continued... The article is not a completed one!.





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Move Rows  .  DataGrid  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: DataGridView Column Header Text with AutoEllipses(...) in C#
Previous Resource: Retrieving value on click of checkbox in DataList C#
Return to Discussion Resource Index
Post New Resource
Category: DataGridView


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use