DataGridView怎样实现添加、删除、上移、下移一行

场景

在Winform中使用DataGridView实现添加一行、删除一行、上移一行、下移一行。

注:

博客主页:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

添加一行

private void TaskViewEditHelper_OnAddStep(object sender, EventArgs e)
        {
          
            DataGridViewRow dr = new DataGridViewRow();
            dr.CreateCells(this.dataGridView_Task_ViewEdit);
            dr.Cells[0].Value = "公众号" + this.dataGridView_Task_ViewEdit.Rows.Count;
            dr.Cells[1].Value = "霸道的程序猿";
            dr.Cells[2].Value = "大量编程教程与资源";
            //this.dataGridView_Task_ViewEdit.Rows.Insert(0, dr);    //添加的行作为第一行
            this.dataGridView_Task_ViewEdit.Rows.Add(dr);//添加的行作为最后一行
        }

效果

 

删除一行

 private void TaskViewEditHelper_OnRemoveStep(object sender, EventArgs e)
        {
            if (this.dataGridView_Task_ViewEdit.SelectedRows == null || this.dataGridView_Task_ViewEdit.SelectedRows.Count == 0)
            {
                XtraMessageBox.Show("请先选择删除步,单击第一列以选中行");
            }
            else
            {
                if (XtraMessageBox.Show("确定要删除选中步吗?") == System.Windows.Forms.DialogResult.OK)
                {
                    foreach (DataGridViewRow dr in this.dataGridView_Task_ViewEdit.SelectedRows)
                    {
                        if (dr.IsNewRow == false)
                        {
                            //如果不是已提交的行,默认情况下在添加一行数据成功后,DataGridView为新建一行作为新数据的插入位置
                            this.dataGridView_Task_ViewEdit.Rows.Remove(dr);
                        }
                    }
                }
            }
        }

效果

 

上移一行

private void TaskViewEditHelper_OnUpStep(object sender, EventArgs e)
        {
          
            if (this.dataGridView_Task_ViewEdit.SelectedRows == null || this.dataGridView_Task_ViewEdit.SelectedRows.Count == 0)
            {
                XtraMessageBox.Show("请先选择一行,单击第一列以选中行");
            }
            else
            {
                if (this.dataGridView_Task_ViewEdit.SelectedRows[0].Index <= 0)
                {
                    XtraMessageBox.Show("此行已在顶端,不能再上移!");
                }
                else
                {
                    //注意:这里是非绑定数据情况的上移行
                    // 选择的行号  
                    int selectedRowIndex = GetSelectedRowIndex(this.dataGridView_Task_ViewEdit);
                    if (selectedRowIndex >= 1)
                    {
                        // 拷贝选中的行  
                        DataGridViewRow newRow = dataGridView_Task_ViewEdit.Rows[selectedRowIndex];
                        // 删除选中的行  
                        dataGridView_Task_ViewEdit.Rows.Remove(dataGridView_Task_ViewEdit.Rows[selectedRowIndex]);
                        // 将拷贝的行,插入到选中的上一行位置  
                        dataGridView_Task_ViewEdit.Rows.Insert(selectedRowIndex - 1, newRow);
                        dataGridView_Task_ViewEdit.ClearSelection();
                        // 选中最初选中的行 
                        dataGridView_Task_ViewEdit.Rows[selectedRowIndex - 1].Selected = true;
                    }
                }
            }
        }

注:

这里是没绑定数据源情况下的上移一行,添加的一行时通过是上面新增的方法实现的。

此时dataGridView的dataSource是为空的。

其中用到获取选中行的方法:

private int GetSelectedRowIndex(DataGridView dgv)
        {
            if (dgv.Rows.Count == 0)
            {
                return 0;
            }
            foreach (DataGridViewRow row in dgv.Rows)
            {
                if (row.Selected)
                {
                    return row.Index;
                }
            }
            return 0;
        }

效果

 

下移一行

        private void TaskViewEditHelper_OnDownStep(object sender, EventArgs e)
        {
            if (this.dataGridView_Task_ViewEdit.SelectedRows == null || this.dataGridView_Task_ViewEdit.SelectedRows.Count == 0)
            {
                XtraMessageBox.Show("请先选择一行,单击第一列以选中行");
            }
            else
            {
                if (this.dataGridView_Task_ViewEdit.SelectedRows[0].Index >= this.dataGridView_Task_ViewEdit.Rows.Count - 1)
                {
                    XtraMessageBox.Show("此行已在底端,不能再下移!");
                }
                else
                {
                    int selectedRowIndex = GetSelectedRowIndex(this.dataGridView_Task_ViewEdit);
                    if (selectedRowIndex < dataGridView_Task_ViewEdit.Rows.Count - 1)
                    {
                        // 拷贝选中的行  
                        DataGridViewRow newRow = dataGridView_Task_ViewEdit.Rows[selectedRowIndex];
                        // 删除选中的行  
                        dataGridView_Task_ViewEdit.Rows.Remove(dataGridView_Task_ViewEdit.Rows[selectedRowIndex]);
                        // 将拷贝的行,插入到选中的下一行位置  
                        dataGridView_Task_ViewEdit.Rows.Insert(selectedRowIndex + 1, newRow);
                        dataGridView_Task_ViewEdit.ClearSelection();
                        // 选中最初选中的行  
                        dataGridView_Task_ViewEdit.Rows[selectedRowIndex + 1].Selected = true;
                    }
                }
            }
          
        }

效果

 

  • 8
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
当我们需要在一个 DataGridView 中对数据进排序时,可以通过上移下移的方式来实现。下面是示例代码,其中包含了详细的注释说明: ```vb.net Private Sub btnUp_Click(sender As Object, e As EventArgs) Handles btnUp.Click '获取当前选中的索引 Dim currentIndex As Integer = dgvData.CurrentRow.Index '如果当前选中的是第一行,则不需要上移 If currentIndex <= 0 Then Return '获取当前选中的数据 Dim currentRow As DataGridViewRow = dgvData.CurrentRow Dim data As Object() = GetDataFromRow(currentRow) '将当前选中的数据移除 dgvData.Rows.RemoveAt(currentIndex) '将当前选中的数据插入到上一行的位置 dgvData.Rows.Insert(currentIndex - 1, data) '选中上移后的 dgvData.CurrentCell = dgvData.Rows(currentIndex - 1).Cells(0) End Sub Private Sub btnDown_Click(sender As Object, e As EventArgs) Handles btnDown.Click '获取当前选中的索引 Dim currentIndex As Integer = dgvData.CurrentRow.Index '如果当前选中的是最后一行,则不需要下移 If currentIndex >= dgvData.Rows.Count - 2 Then Return '获取当前选中的数据 Dim currentRow As DataGridViewRow = dgvData.CurrentRow Dim data As Object() = GetDataFromRow(currentRow) '将当前选中的数据移除 dgvData.Rows.RemoveAt(currentIndex) '将当前选中的数据插入到下一行的位置 dgvData.Rows.Insert(currentIndex + 1, data) '选中下移后的 dgvData.CurrentCell = dgvData.Rows(currentIndex + 1).Cells(0) End Sub Private Function GetDataFromRow(row As DataGridViewRow) As Object() '从 DataGridViewRow 中获取数据 Dim data As New List(Of Object) For Each cell As DataGridViewCell In row.Cells data.Add(cell.Value) Next Return data.ToArray() End Function ``` 这段代码中,我们定义了两个按钮 btnUp 和 btnDown,分别用于上移下移选中的。在 btnUp_Click 和 btnDown_Click 事件中,我们首先获取当前选中的索引,判断是否需要上移下移,然后获取当前选中的数据,将其从 DataGridView 中移除,并插入到上一行或下一行的位置。最后,通过设置 dgvData.CurrentCell 选中上移下移后的。GetDataFromRow 函数用于从 DataGridViewRow 中获取数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸道流氓气质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值