Reorder Gridview JQuery : Drag Drop reorder of GridView Rows

satinder singh / Updated : / 17 Comments


Reorder Gridview: This article explains how to reorder or sort Gridview control rows  .i.e. drag and drop row for sorting or change row position order of Gridview control with jQuery in c#.

For the drag-drop feature here we use jQuery UI sortable, using this enables drag and drop feature on our Gridview control.

Basically, this makes our Gridview drag and drop rows to change their row positions.

Reorder Gridview JQuery : Drag Drop reorder of GridView Rows

# HTML Markup :

Here we added Gridview control and Button control. Also need to import some js file like JQuery latest lib, jQuery Ui.js , and jQuery ui.css.

So finally our HTML markup look like as given below

            <asp:GridView ID="gvLanguages" CssClass="gvLanguageClass" runat="server" AutoGenerateColumns="False">
                    <asp:TemplateField HeaderText="Sr no" ItemStyle-Width="50">
                            <%# Container.DataItemIndex + 1 %>
                            <input type="hidden" name="languageId" value='<%# Eval("id_") %>' />
                    <asp:BoundField DataField="language_name" HeaderText="Language Name" ItemStyle-Width="160" />
                    <asp:BoundField DataField="developed_by" HeaderText="Developed By" ItemStyle-Width="160" />
                    <asp:BoundField DataField="sort_order" HeaderText="Sort order" ItemStyle-Width="50" />
            <br />
            <asp:Button ID="btnUpdateSortOrder" runat="server" Text="Update Sort Order"
                OnClick="btnUpdateSortOrder_Click" />

# DataBase:

We having a table named as  programming_language_info where we have 4 columns  id_, language_name,  developed_by, sort_order.

CREATE TABLE [dbo].[programming_language_info](
	[id_] [bigint] IDENTITY(1,1) NOT NULL,
	[language_name] [nvarchar](150) NULL,
	[developed_by] [nvarchar](150) NULL,
	[sort_order] [bigint] NULL

# Code Behind:

First we create method as bindGridview,  this method will bind our Gridview control from the database, and will call this method on page_load  event.

protected void Page_Load(object sender, EventArgs e)
  if (!Page.IsPostBack) {


public void bindGridview() 
   string myQuery = "select id_,language_name,developed_by,sort_order from programming_language_info order by sort_order";
   SqlDataAdapter dap = new SqlDataAdapter(myQuery,cn);
   DataSet ds = new DataSet();
   gvLanguages.DataSource = ds.Tables[0];

So now hit F5 and you will see our Gridview is populated with data 🙂


# Jquery:  apply jQuery sortable to enable drag drop reorder feature on Gridview.

Currently, our Gridview is populated with data, but you will find drag and drop functionality is not there.

To add drag-drop functionality on our gridview we are using jQuery UI sortable.

So we can reorder or sort Gridview rows by just simple drag and drop row of Gridview control.

    items: 'tr:not(tr:first-child)',
    cursor: 'pointer',
    axis: 'y',
    dropOnEmpty: false,
    start: function (e, ui) {
    stop: function (e, ui) {

Yeah, we are done,  Hit F5 and now you will able to drag and drop Gridview row, i.e reorder of Gridview row.

But their a twist we are able to drag-drop and reorder the rows but not saving the changed rows ordering update database. So now we are going to save the changed ordering into our database.

# Code Behind: To save position of reordered rows of Gridview into the database.

protected void btnUpdateSortOrder_Click(object sender, EventArgs e)
    // stores id_ in array
    string [] id_language =Request.Form.GetValues("languageId");
    int sortNumber=1;
    // Loop over array, which contains id_ 
    foreach (string i in id_language) {
         // method which which fire update query order save into database
         updateRecord(i, sortNumber); 


  public void updateRecord(string languageId,int sortOrder) 
        string updateQuery = "UPDATE programming_language_info SET sort_order = @sort_order WHERE id_ = @id_";
        using (SqlConnection conn = new SqlConnection(cn.ConnectionString))
            using (SqlCommand cmd = new SqlCommand()) {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = updateQuery;
                cmd.Connection = conn;
                cmd.Parameters.AddWithValue("@id_", languageId);

Output :

Reorder Gridview JQuery : Drag Drop reorder of GridView Rows


You can also check:

  1. Sorting Gridview control on header click with pagination
  2. Gridview row color change based on data c#.
  3. Gridview row mouseover show detail data in floating div c# jQuery.
  4. Import Excel Sheet Data to Gridview C-sharp.

Other Resource:

  1. Drag and Drop GridView Rows using Jquery tableDnD Plugin

Hope you enjoyed this tutorial. If you have any recommendations, please let us know what you think in the comment section below! See you again next time!

17 comments on “Reorder Gridview JQuery : Drag Drop reorder of GridView Rows”

  1. David

    Hi could someone please provide a link to download the code?

    I followed the steps but I am getting the below error when running.

    Fill: SelectCommand.Connection property has not been initialized.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: Fill: SelectCommand.Connection property has not been initialized.

    Source error:
    Line 29: dap.Fill(ds);

  2. Vishal Mistry

    Hi, this looks good.

    I have a query before we actually implement; Does this work on touch screen – If I have a site with such feature then will it also work on tablet/phone?

  3. Brad

    Nice article, I using this in my dev site and works beautifully. my question though is do the field have to be a boundfield? after I changed to item and edit templates, Request.Form.GetValues(“languageid”) has “nothing” in it???

  4. Boyd

    Very nice article. I initially just had a field which incremented, but it was not very intuitive or user friendly. This is so much better!

    • Vam

      you will face issues with updating Db directly, I am moving to button from direct update, you can use JSON and web services to achieve what you are looking for

  5. Esb

    not judging or anything but I don’t see a practical value of reordering rows. If you explain how to reorder columns on drag and drop, that’d be much more interesting

  6. Peter

    Thank u so much for explaining article on rearranging order of GridView Rows using drag and drop.
    its help me alot

  7. Richard David

    Excellent post.
    Very good use of jquery ui sort-able for dragging and reorder the Gridview rows. Can you also provide the Solution file web pages

Post Comment

Your email address will not be published. Required fields are marked *