简体   繁体   English

如何根据单击按钮向上或向下移动 datagridview 行

[英]How to move datagridview rows up or down based on a button click

I have DataGridView which is bounded with a datatable.我有与数据表绑定的 DataGridView。 I fill this datatable from database.我从数据库中填充这个数据表。 I have 2 buttons right next to the datagridview, Up and Down.我在 datagridview 旁边有 2 个按钮,向上和向下。 the rows should move up and down based on whichever button i click.行应根据我单击的任何按钮上下移动。 I understand there are many answers for the similar issues but they work if your gridview isnt bounded.我知道类似问题有很多答案,但如果您的 gridview 不受限制,它们就会起作用。 I also have a sequence column which has numbers from 0 onwards.我还有一个序列列,其中包含从 0 开始的数字。 When the rows move up or down, the sequence should be fixed as well.当行向上或向下移动时,顺序也应该固定。 For example, if i move row[12] up, the sequence of row[12] should update to row[11] and row[11] should move down and row[11] sequence becomes row[12].例如,如果我向上移动 row[12],row[12] 的序列应该更新为 row[11],row[11] 应该向下移动,row[11] 序列变成 row[12]。 (I'm sorry if this is confusing). (如果这令人困惑,我很抱歉)。 I also have 2 event functions attached to up and down buttons as well.我也有 2 个事件函数附加到向上和向下按钮。

Edit : The main goal of this gridview is that user can add new rows as well.编辑:这个 gridview 的主要目标是用户也可以添加新行。 So, along with the above information, if there are 5 rows with sequence 1 to 5, and the user adds another row with sequence 2. I can sort the rows but how do I make the origninal rows with sequence 2,3,4,5 shift down and change their sequence to 3,4,5,6?因此,连同上述信息,如果有 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);
    }

There are definitely some loopholes in my explanation so please be kind.我的解释肯定有一些漏洞,所以请善待。 I will do my best to explain any confusions, if need be.如果需要,我会尽力解释任何混淆。

If your rows are bound, then following example is applicable for moving row up. 如果行已绑定,则以下示例适用于向上移动行。 Prober Bounding should take care about sorting the DataGridView on the screen. Prober Bounding应该注意在屏幕上对DataGridView进行排序。 If you sorted it in the data bound source, then it should work without .Sort , I think (I didn't test that). 如果您在数据绑定源中对它进行排序,那么我认为它应该在没有.Sort情况下也可以工作(我没有对此进行测试)。

If the row numbering could be inconsistent (c#): 如果行编号可能不一致(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: 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

If the rownumbering is consistent (continuous 1 to N), then it can be simplified: 如果行编号是一致的(从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: 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

The moving down is similar, just the condition takes into account a limit of number of rows. 向下移动类似,只是条件考虑了行数的限制。

While this worked for me in similar scenario, I usually don't use binding in such scenarios, I use a DataTable as a data mediator. 尽管这在类似情况下对我有用,但通常在这种情况下不使用绑定,而是将DataTable用作数据中介器。

In mine I load from the datagridview with a mysql query, I'm not able to make it go down or up line by line在我的中,我使用 mysql 查询从 datagridview 加载,我无法使其逐行下降或上升

enter image description here在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM