Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6097
  • Last Modified:

how to move DataGridView selected row up and down?

hi,

I have datagridview in c#2008, my client wants to have two buttons one to move the current selected record up (one step) and the other button would be to move the current selected record down

        private void btnSelectProductsOrdDown_Click(object sender, EventArgs e)
        {

        }
        private void btnSelectProductsOrdUp_Click(object sender, EventArgs e)
        {

        }

so how can I do that with myDataGridView?
0
njgroup
Asked:
njgroup
3 Solutions
 
CodeCruiserCommented:
Check this one. Code is in VB but you can convert it to C# using an online tool

http://www.vbforums.com/showthread.php?t=508749
0
 
nandithaaCommented:
Something like this might help:

  private void btnSelectProductsOrdDown_Click(object sender, EventArgs e)
        {

            int inIndex = dataGridView1.CurrentRow.Index;
            if (inIndex != (dataGridView1.Rows.Count - 1))
            {
                DataGridViewRow dr = dataGridView1.Rows[inIndex];
                dataGridView1.Rows.RemoveAt(inIndex);
                dataGridView1.Rows.Insert(inIndex + 1, dr);
                dataGridView1.CurrentCell = dataGridView1.Rows[inIndex + 1].Cells[0];
            }
        }
        private void btnSelectProductsOrdUp_Click(object sender, EventArgs e)
        {
            int inIndex = dataGridView1.CurrentRow.Index;
            
            if (inIndex != 0)
            {
                DataGridViewRow dr = dataGridView1.Rows[inIndex];
                dataGridView1.Rows.RemoveAt(inIndex);
                dataGridView1.Rows.Insert(inIndex - 1, dr);
                dataGridView1.CurrentCell = dataGridView1.Rows[inIndex - 1].Cells[0];
            }
        }

Open in new window

0
 
Deepu SreedharBI Software EngineerCommented:
Do you want to move the selection or actually move the row?
If you want to move the selection you can use the below code.
private void cmdUp_Click(System.Object sender, System.EventArgs e)
{
	if (DGView.CurrentCell.RowIndex > 0) {
		DGView.CurrentCell = DGView(DGView.CurrentCell.ColumnIndex, DGView.CurrentCell.RowIndex - 1);
	}
}
private void cmdDown_Click(System.Object sender, System.EventArgs e)
{
	if (DGView.CurrentCell.RowIndex < DGView.RowCount - 1) {
		DGView.CurrentCell = DGView(DGView.CurrentCell.ColumnIndex, DGView.CurrentCell.RowIndex + 1);
	}
}

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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