Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swap rows in datagridview in c#

Tags:

c#

rows


i have a datagridview which is not related with dataTable.
and i want to swap for example 1st and 10th rows in datagridview.
i use this code for it

int row_1 = 1;
int row_10 = 10;
for(int i=0;i<grid.ColumnCount;i++)
{
  string temp = grid.Rows[row_1].Cells[i].Value.ToString();
  grid.Rows[row_1].Cells[i].Value = grid.Rows[row_10].Cells[i].Value.ToString();
  grid.Rows[row_10].Cells[i].Value = temp;
}

but i want to know is there any simple way to do this??

like image 871
namco Avatar asked Apr 15 '11 11:04

namco


2 Answers

var r10 = grid.Rows[9];
var r1 = grid.Rows[0];
grid.Rows.Remove(r1);
grid.Rows.Remove(r10);
grid.Rows.Insert(0, r10);
grid.Rows.Insert(9, r1);
like image 133
DarkSquirrel42 Avatar answered Sep 18 '22 12:09

DarkSquirrel42


HI,
Have you tried:

DataGridViewRow temp =grid.Rows[row_1];
grid.Rows[row_1] = grid.Rows[row_10];
grid.Rows[row_10] = temp;
like image 32
Gabriel Avatar answered Sep 22 '22 12:09

Gabriel