【问题标题】:How to move datagridview rows up or down based on a button click如何根据按钮单击向上或向下移动 datagridview 行
【发布时间】:2019-07-23 16:17:10
【问题描述】:

我有与数据表绑定的 DataGridView。我从数据库中填充这个数据表。我在datagridview旁边有2个按钮,向上和向下。行应根据我单击的按钮上下移动。我知道类似问题有很多答案,但如果您的 gridview 没有界限,它们会起作用。我还有一个序列列,其中的数字从 0 开始。当行向上或向下移动时,顺序也应该是固定的。例如,如果我将 row[12] 向上移动,则 row[12] 的序列应该更新为 row[11] 并且 row[11] 应该向下移动并且 row[11] 序列变为 row[12]。 (如果这令人困惑,我很抱歉)。我还为向上和向下按钮附加了 2 个事件函数。

编辑:此网格视图的主要目标是用户也可以添加新行。因此,连同上述信息,如果有 5 行序列为 1 到 5,并且用户添加了序列为 2 的另一行。我可以对行进行排序,但我如何制作序列为 2、3、4 的原始行, 5 下移并改变它们的顺序为 3,4,5,6?

private void moveRow(int position)
    {
        DataRow selectedDataRow = null;
        DataRow newDataRow = null;
        int sequence = -1;
        int newSequence = -1;
        DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
        sequence = (int)selectedRow.Cells[sequenceDataGridViewTextBoxColumn.Index].Value;
        newSequence = sequence + position;
        if (newSequence <= 0 || newSequence > dataGridView.Rows.Count)
        {
            return;
        }



        //below code doesnt work at all maybe cuz it isnt right
        //How do i select the current row and the row at the new sequence?????



            if (selectedDataRow != null && newDataRow != null)
            {//below i try to assign sequences to the rows
                selectedDataRow["Sequence"] = newSequence;
                newDataRow["Sequence"] = sequence;
            }
            dataGridViewRed.Sort(dataGridViewRed.Columns[buildSequenceDataGridViewTextBoxColumn.Index], ListSortDirection.Ascending);// i sort it again based on sequence
            dataGridViewRed.CurrentCell = dataGridViewRed.Rows[selectedRow.Index + position].Cells[buildSequenceDataGridViewTextBoxColumn.Index];//highlight the current selected cell
//below functions are the events attached to the buttons for up and down
private void UpBtn_Click(object sender, EventArgs e)
    {
        moveRow(-1);
    }

    private void DownBtn_Click(object sender, EventArgs e)
    {
        moveRow(+1);
    }

我的解释肯定有一些漏洞,所以请多多指教。如果需要,我会尽力解释任何混淆。

【问题讨论】:

  • 为什么不按序列值排序并保持它而不是尝试管理 bound 控件中的行?
  • 让我编辑我的问题。我忘记了一个重点。对不起。
  • 当他们添加时,遍历数据源并更新 2 或以上的 ID。如果您使用正确的集合类型,DGV 将自行(重新)排序。您不应该尝试在绑定控件中管理 DGV 行

标签: c# visual-studio-2015 datagridview


【解决方案1】:

如果您的行已绑定,则以下示例适用于向上移动行。 Prober Bounding 应该注意对屏幕上的DataGridView 进行排序。如果您在数据绑定源中对其进行排序,那么它应该可以在没有.Sort 的情况下工作,我认为(我没有测试过)。

如果行号可能不一致(c#):

DataGridViewRow SelectedDataRow = ...;
if (SelectedDataRow.Index > 0)
{
    DataGridViewRow PrevDataRow = DataGridView1.Rows(SelectedDataRow.Index - 1);
    Int16 PrevSequence = PrevDataRow.Cells("Sequence").Value;  // buffer the previous sequence
    PrevDataRow.Cells("Sequence").Value = SelectedDataRow.Cells("Sequence").Value;    // set previous sequence to selected
    SelectedDataRow.Cells("Sequence").Value = PrevSequence;                           // set selected to previous
} 

VB.NET:

        Dim SelectedDataRow As DataGridViewRow = ...
        If SelectedDataRow.Index > 0 Then
            Dim PrevDataRow As DataGridViewRow = DataGridView1.Rows(SelectedDataRow.Index - 1)
            Dim PrevSequence As Int16 = PrevDataRow.Cells("Sequence").Value  ' buffer the previous sequence
            PrevDataRow.Cells("Sequence").Value = SelectedDataRow.Cells("Sequence").Value    ' set previous sequence to selected
            SelectedDataRow.Cells("Sequence").Value = PrevSequence                           ' set selected to previous
        End If

如果行号一致(从1到N连续),那么可以简化:

DataGridViewRow SelectedDataRow = ...;
if (SelectedDataRow.Index > 0)
{
    DataGridViewRow PrevDataRow = DataGridView1.Rows(SelectedDataRow.Index - 1);
    PrevDataRow.Cells("Sequence").Value = SelectedDataRow.Index + 1;
    SelectedDataRow.Cells("Sequence").Value = SelectedDataRow.Index;                            // set selected to previous
}

VB.NET:

        Dim SelectedDataRow As DataGridViewRow = ...
        If SelectedDataRow.Index > 0 Then
            Dim PrevDataRow As DataGridViewRow = DataGridView1.Rows(SelectedDataRow.Index - 1)
            PrevDataRow.Cells("Sequence").Value = SelectedDataRow.Index + 1
            SelectedDataRow.Cells("Sequence").Value = SelectedDataRow.Index                            ' set selected to previous
        End If

下移类似,只是条件考虑了行数限制。

虽然这在类似情况下对我有用,但在这种情况下我通常不使用绑定,我使用 DataTable 作为数据中介。

【讨论】:

  • 您的答案有效。但我不得不将PrevDataRow.Cells("Sequence").Value = SelectedDataRow.Index + 1; SelectedDataRow.Cells("Sequence").Value = SelectedDataRow.Index; 更改为以下内容:PrevDataRow.Cells("Sequence").Value = SelectedDataRow.Index; SelectedDataRow.Cells("Sequence").Value = SelectedDataRow.Index+1;
  • 抱歉,前者用于从 0 开始的行编号。很高兴它有点帮助。
【解决方案2】:

在我的中,我使用 mysql 查询从 datagridview 加载,我无法让它逐行向下或向上

enter image description here

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
  • 我忘了说它在 vb.net 上
猜你喜欢
  • 2019-08-10
  • 2012-03-05
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2020-10-20
  • 2013-06-23
  • 2016-01-06
  • 1970-01-01
相关资源
最近更新 更多