I'm trying to sort the records at a dataset according to users preference. I add a button (move up) to bump up the selected record 1 row higher and this is what i have done until now:
private void btnMoveUp_Click(object sender, EventArgs e)
{
int index = dataGridView1.CurrentRow.Index;
if (index >= 1)
{
var temp = dsWinners.Tables[0].Rows[index];
dsWinners.Tables[0].Rows[index].Delete();
dsWinners.Tables[0].Rows.InsertAt(temp, index - 1);
dsWinners.Tables[0].AcceptChanges();
dataGridView1.DataSource = dsWinners.Tables[0];
}
}
but i get a error saying "This row already belongs to this table." at this line:
dsWinners.Tables[0].Rows.InsertAt(temp, index - 1);
i know that i have to use ImportRow(temp) instead of InsertAt(temp, index - 1) but the problem is i dont know how to import it at the right position. i have looked int to THIS but as the guy who answered the question says "Its sloppy and halfway."
is there anyway i can overcome this issue? and if yes, how can i do it?
Try below things :