moving selected rows up and down in datagridview by button click

hi
(c# windows form)
i have  some rows in my datagrid i just want to move the selected rows up or down using the up down buttons(which i had in my form)
can any one provide the sample code
thank you
kranthi4uonlyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hamidovtCommented:
I can not write a comple code for your because it will be quite a lot:) But I can explain you what you need to do and provide some samples...

- You should add order column to your dataset (for example if you have Id, Name table add an Order column to it so that the dataset structure is Id, Name, Order)

- Add Up and Down buttons to your datagrid. Something like:
<asp:TemplateField>
  <ItemTemplate>
    <asp:ImageButton id="imbUp" runat="server" ImageUrl="~/images/Up.gif" CommandName="up" CommandArgument='<%# Eval("Id") %>' />
  </ItemTemplate>
</asp:TemplateField>

- Add _RowCommand method to your grid and if the command is "up" change the order of your dataset records and rebind your grid. Something like this:

protected void Grid1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    switch (e.CommandName)
    {
        case "up" :
            //change order
            int id = int.Parse(e.CommandArgument.ToString());
            //substruct 1 from order of this record and also adapt the order of other records
            ...
            break;
        case "down" :
            //change order
            int id = int.Parse(e.CommandArgument.ToString());
            //add 1 to order of this record and also adapt the order of other records
            ...
            break;
    }
    Grid1.DataBind();
}
0
kranthi4uonlyAuthor Commented:
hi
thanks for ur reply my grid is not data bound and more over its windows app
but i tried this code

it is moving the row down iam swaping the rows but the problem is the focus is not going to the next row
for example if i click on row 2 and click down button the row is getting swapped i.e row 2 is becoming 3 and 3 is becoming 2 but the focus is still on row 2 i want the focus to be moved to row 3 ..
so how to move the focus to 3 rd row thank you
private void downbutton1_Click(object sender, EventArgs e)
        {
            for (int j = 0; j < this.dataGridView1.Columns.Count; j++)
            {
 
                object tmp = this.dataGridView1[j,dataGridView1.SelectedRows[0].Index].Value;
 
 this.dataGridView1[j, dataGridView1.SelectedRows[0].Index].Value = this.dataGridView1[j, dataGridView1.SelectedRows[0].Index+1].Value;
 
                this.dataGridView1[j, dataGridView1.SelectedRows[0].Index+1].Value = tmp;
               
 
            }
        }

Open in new window

0
hamidovtCommented:
sorry, I am not very experiences with datagrid in windows:(
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.