• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7753
  • Last Modified:

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
0
kranthi4uonly
Asked:
kranthi4uonly
  • 2
1 Solution
 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now