在 C# 中的 datagridview 中交换行

发布于 2024-11-01 17:45:28 字数 408 浏览 6 评论 0原文


我有一个与 dataTable 无关的 datagridview。
我想交换例如 datagridview 中的第一行和第十行。
我使用这段代码,

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;
}

但我想知道有没有简单的方法可以做到这一点?

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??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

疾风者 2024-11-08 17:45:28
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);
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);
再可℃爱ぅ一点好了 2024-11-08 17:45:28

嗨,
你有没有尝试过:

DataGridViewRow temp =grid.Rows[row_1];
grid.Rows[row_1] = grid.Rows[row_10];
grid.Rows[row_10] = temp;

HI,
Have you tried:

DataGridViewRow temp =grid.Rows[row_1];
grid.Rows[row_1] = grid.Rows[row_10];
grid.Rows[row_10] = temp;
野の 2024-11-08 17:45:28

我想对此发表评论。

虽然以前可能可以在 C# 中进行直接交换,例如 smoore/Gabriels,

DataGridViewRow temp = grid.Rows[row_1].Clone();
grid.Rows[row_1] = grid.Rows[row_10].Clone();
grid.Rows[row_10] = temp;

但现在不再可能了,因为 grid.Rows[index] 是只读的。

相反,使用 DarkSquirrels 方法保存两行,删除行,然后重新插入交换的行。

如果有人知道更好的方法(因为这就像 iteslf 的 6 行方法,没有找到其他值的逻辑)请发表评论!

I wanted to comment on this.

While it -may- have been possible to do a straight swap in C# before, like smoore/Gabriels-

DataGridViewRow temp = grid.Rows[row_1].Clone();
grid.Rows[row_1] = grid.Rows[row_10].Clone();
grid.Rows[row_10] = temp;

This is no longer possible, as grid.Rows[index] is read only.

Instead, use DarkSquirrels method of saving the two rows, removing the rows, and re-inserting them swapped.

If somebody knows a better method (as this is like a 6 line method by iteslf without the logic to find the other value) please do comment!

谁对谁错谁最难过 2024-11-08 17:45:28

尝试:

DataGridViewRow temp = grid.Rows[row_1].Clone();
grid.Rows[row_1] = grid.Rows[row_10].Clone();
grid.Rows[row_10] = temp;

Try:

DataGridViewRow temp = grid.Rows[row_1].Clone();
grid.Rows[row_1] = grid.Rows[row_10].Clone();
grid.Rows[row_10] = temp;
橘味果▽酱 2024-11-08 17:45:28

就像补充一样:

如果您需要更频繁地交换,请将您最喜欢的上面的方法放在自己的类中并调用该方法(例如 Interchange())

Just as addition:

if you need interchange more often, put the method above you prefer most in its own class and call the method (e.g. Interchange())

孤星 2024-11-08 17:45:28

这个方法效果很好:

private static void SwapRows(DataGridView grid, int row1, int row2)
{
  if (row1 != row2 && row1 >= 0 && row2 >= 0 && row1 < grid.RowCount && row2 < grid.RowCount)  
  {
    var rows = grid.Rows.Cast<DataGridViewRow>().ToArray();
    var temp = rows[row1];
    rows[row1] = rows[row2];
    rows[row2] = temp;
    grid.Rows.Clear();
    grid.Rows.AddRange(rows);
  }
}

This method works fine:

private static void SwapRows(DataGridView grid, int row1, int row2)
{
  if (row1 != row2 && row1 >= 0 && row2 >= 0 && row1 < grid.RowCount && row2 < grid.RowCount)  
  {
    var rows = grid.Rows.Cast<DataGridViewRow>().ToArray();
    var temp = rows[row1];
    rows[row1] = rows[row2];
    rows[row2] = temp;
    grid.Rows.Clear();
    grid.Rows.AddRange(rows);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文