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?
Instead of removing the row and adding it again at another position, you could add a column to the table that contains the position.
You'd not bind the DataGridView to the table itself but to a DataView that is sorted by the Position column.
In the DataGridView, you'd add all the columns except the Position so that the Position is not displayed.
When you move a row up or down, you swap the value of the Position cell with the previous or next row respectively. As the DataView is sorted by the position, the move is immediately reflected in the DataView.
The following console application shows the basic steps:
void Main()
{
// Create a new DataTable
DataTable tbl = GetData();
// Add Position column and initialize it
tbl.Columns.Add("Position", typeof(int));
for(int i = 0; i < tbl.Rows.Count; i++)
tbl.Rows[i]["Position"] = i;
PrintView("Original", tbl.DefaultView);
// Create a view that is sorted by the Position column
var view = new DataView(tbl);
view.Sort = "Position";
PrintView("Sorted", view); // No change as the rows are in original order anyway
// Move a row
SwapPositions(view, 0, 1); // Move first row down
PrintView("Swapped", view);
}
private DataTable GetData()
{
DataTable tbl = new DataTable();
tbl.Columns.Add("Value", typeof(string));
for(int i = 0; i < 3; i++)
tbl.Rows.Add("Text " + i.ToString());
return tbl;
}
private void PrintView(string header, DataView view)
{
Console.WriteLine(header);
foreach(DataRowView row in view)
Console.WriteLine("{0}\t{1}", row["Position"], row["Value"]);
}
private void SwapPositions(DataView view, int rowIndex1, int rowIndex2)
{
// Save rows as the change in the Position column is reflected immediately
DataRowView row1 = view[rowIndex1];
DataRowView row2 = view[rowIndex2];
// Now swap positions
var saveValue = row1["Position"];
row1["Position"] = row2["Position"];
row2["Position"] = saveValue;
view.Table.AcceptChanges();
}
The output is:
Original
0 Text 0
1 Text 1
2 Text 2
Sorted
0 Text 0
1 Text 1
2 Text 2
Swapped
0 Text 1
1 Text 0
2 Text 2
Try below things :
var temp = dsWinners.Tables[0].Rows[index]; \\getting row
var newrow = dsWinners.Tables[0].NewRow(); \\ creating new row to insert
newrow.ItemArray = temp.ItemArray; \\ copying data from old to new row
dsWinners.Tables[0].Rows[index].Delete(); \\deleting old row.
dsWinners.Tables[0].Rows.InsertAt(newrow, index - 1); \\ adding new row.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With