in case if you have up and down buttons in your grid rows
<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;
}
}
}