Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am bind some data in gridview ,i want to move(rowss Data) up/down in gridview ,can anyone help me how to do. thankyou.
Posted
Comments
Khandwawala Hatim 11-Oct-13 7:06am    
are you having any up and down button on the web form or user will use Key board up and down keys to move the selected row of the grid up/down?

 
Share this answer
 
in case if you have up and down buttons in your grid rows

XML
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" OnRowCommand="gv_RowCommand">
     <Columns>
         <asp:TemplateField>............</asp:TemplateField>
     <asp:TemplateField>............</asp:TemplateField>
     <asp:TemplateField>............</asp:TemplateField>
         <asp:TemplateField>
             <ItemTemplate>
                 <asp:Button ID="btnDown" runat="server"
                     CausesValidation="false"
                     CommandName="Down" Text="↓" />
             </ItemTemplate>
         </asp:TemplateField>
     </Columns>
</asp:GridView>


protected void gvQuestion_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    try
    {
       // Handle the Up command
       if (e.CommandName == "Up")
       {
           moveUp()
       }

       // Handle the Down command
       if (e.CommandName == "Down")
       {
           moveDown()
       }
}



private void moveUp()
    {
        if (dataGridView1.RowCount > 0)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                int rowCount = dataGridView1.Rows.Count;
                int index = dataGridView1.SelectedCells[0].OwningRow.Index;

                if (index == 0)
                {
                    return;
                }
                DataGridViewRowCollection rows = dataGridView1.Rows;

                // remove the previous row and add it behind the selected row.
                DataGridViewRow prevRow = rows[index - 1];
                rows.Remove(prevRow);
                prevRow.Frozen = false;
                rows.Insert(index, prevRow);
                dataGridView1.ClearSelection();
                dataGridView1.Rows[index - 1].Selected = true;
            }
        }
    }

    private void moveDown()
    {
        if (dataGridView1.RowCount > 0)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                int rowCount = dataGridView1.Rows.Count;
                int index = dataGridView1.SelectedCells[0].OwningRow.Index;

                if (index == (rowCount - 2)) // include the header row
                {
                    return;
                }
                DataGridViewRowCollection rows = dataGridView1.Rows;

                // remove the next row and add it in front of the selected row.
                DataGridViewRow nextRow = rows[index + 1];
                rows.Remove(nextRow);
                nextRow.Frozen = false;
                rows.Insert(index, nextRow);
                dataGridView1.ClearSelection();
                dataGridView1.Rows[index + 1].Selected = true;
            }
        }
    }
 
Share this answer
 
HTML
<blockquote class="FQ"><div class="FQA">Quote:</div><html>
<head>

    <script type="text/javascript" language="javascript" src="jquery.js"></script>

    <script type="text/javascript" language="javascript">
        $(document).ready(function() {
            $("tr").on("click", function() {
            
                $("[selr=Y]").css({"background-color":"white"})
                $("[selr=Y]").css({"color":"black"})
                $("[selr=Y]").removeAttr("selr");
                
                $(this).css({"background-color":"red"})
                $(this).css({"color":"white"})
                
                $(this).attr({"selr":"Y"})
            });
        });
        function MoveUp() {
            if($("[selr=Y]").length==0) {
                alert("Please select a row to move !");
                return;
            }
            try{
                tbl.moveRow($("[selr=Y]")[0].rowIndex,$("[selr=Y]")[0].rowIndex-1);
            }catch(e){}
        }
        function MoveDown() {
            if($("[selr=Y]").length==0) {
                alert("Please select a row to move !");
                return;
            }
            try{
                tbl.moveRow($("[selr=Y]")[0].rowIndex,$("[selr=Y]")[0].rowIndex+1);
            }catch(e){}
        }
    </script>
</head>
<body>
    <center>
        <table id="tbl" border="1" cellpadding="4" cellspacing="1">
            <tr>
                <td>
                    DINESH1</td>
            </tr>
            <tr>
                <td>
                    DINESH2</td>
            </tr>
            <tr>
                <td>
                    DINESH3</td>
            </tr>
            <tr>
                <td>
                    DINESH4</td>
            </tr>
            <tr>
                <td>
                    DINESH5</td>
            </tr>
            <tr>
                <td>
                    DINESH6</td>
            </tr>
            <tr>
                <td>
                    DINESH7</td>
            </tr>
            <tr>
                <td>
                    DINESH8</td>
            </tr>
            <tr>
                <td>
                    DINESH9</td>
            </tr>
            <tr>
                <td>
                    DINESH10</td>
            </tr>
        </table>
        <br />
        <br />
        <br />
        <input type="button" value="Move Up" onclick="MoveUp();" />
        <input type="button" value="Move Down" onclick="MoveDown();" />
    </center>
    
</body>
</html</blockquote>>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900