请选择 进入手机版 | 继续访问电脑版

马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

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

[复制链接]
查看4706 | 回复3 | 2019-11-8 09:53:52 | 显示全部楼层 |阅读模式

场景

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

注:

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

实现

添加一行

  1. private void TaskViewEditHelper_OnAddStep(object sender, EventArgs e)
  2. {
  3. DataGridViewRow dr = new DataGridViewRow();
  4. dr.CreateCells(this.dataGridView_Task_ViewEdit);
  5. dr.Cells[0].Value = "公众号" + this.dataGridView_Task_ViewEdit.Rows.Count;
  6. dr.Cells[1].Value = "霸道的步伐猿";
  7. dr.Cells[2].Value = "大量编程教程与资源";
  8. //this.dataGridView_Task_ViewEdit.Rows.Insert(0, dr); //添加的行作为第一行
  9. this.dataGridView_Task_ViewEdit.Rows.Add(dr);//添加的行作为末了一行
  10. }
复制代码

效果

095615wvvuqru6rqfn44xf.gif

删除一行

  1. private void TaskViewEditHelper_OnRemoveStep(object sender, EventArgs e)
  2. {
  3. if (this.dataGridView_Task_ViewEdit.SelectedRows == null || this.dataGridView_Task_ViewEdit.SelectedRows.Count == 0)
  4. {
  5. XtraMessageBox.Show("请先选择删除步,单击第一列以选中行");
  6. }
  7. else
  8. {
  9. if (XtraMessageBox.Show("确定要删除选中步吗?") == System.Windows.Forms.DialogResult.OK)
  10. {
  11. foreach (DataGridViewRow dr in this.dataGridView_Task_ViewEdit.SelectedRows)
  12. {
  13. if (dr.IsNewRow == false)
  14. {
  15. //假如不是已提交的行,默认情况下在添加一行数据成功后,DataGridView为新建一行作为新数据的插入位置
  16. this.dataGridView_Task_ViewEdit.Rows.Remove(dr);
  17. }
  18. }
  19. }
  20. }
  21. }
复制代码

效果

095616edbpb8s5beh5vqiq.gif

上移一行

  1. private void TaskViewEditHelper_OnUpStep(object sender, EventArgs e)
  2. {
  3. if (this.dataGridView_Task_ViewEdit.SelectedRows == null || this.dataGridView_Task_ViewEdit.SelectedRows.Count == 0)
  4. {
  5. XtraMessageBox.Show("请先选择一行,单击第一列以选中行");
  6. }
  7. else
  8. {
  9. if (this.dataGridView_Task_ViewEdit.SelectedRows[0].Index <= 0)
  10. {
  11. XtraMessageBox.Show("此行已在顶端,不能再上移!");
  12. }
  13. else
  14. {
  15. //留意:这里好坏绑定命据情况的上移行
  16. // 选择的行号
  17. int selectedRowIndex = GetSelectedRowIndex(this.dataGridView_Task_ViewEdit);
  18. if (selectedRowIndex >= 1)
  19. {
  20. // 拷贝选中的行
  21. DataGridViewRow newRow = dataGridView_Task_ViewEdit.Rows[selectedRowIndex];
  22. // 删除选中的行
  23. dataGridView_Task_ViewEdit.Rows.Remove(dataGridView_Task_ViewEdit.Rows[selectedRowIndex]);
  24. // 将拷贝的行,插入到选中的上一行位置
  25. dataGridView_Task_ViewEdit.Rows.Insert(selectedRowIndex - 1, newRow);
  26. dataGridView_Task_ViewEdit.ClearSelection();
  27. // 选中最初选中的行
  28. dataGridView_Task_ViewEdit.Rows[selectedRowIndex - 1].Selected = true;
  29. }
  30. }
  31. }
  32. }
复制代码

注:

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

此时dataGridView的dataSource是为空的。

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

  1. private int GetSelectedRowIndex(DataGridView dgv)
  2. {
  3. if (dgv.Rows.Count == 0)
  4. {
  5. return 0;
  6. }
  7. foreach (DataGridViewRow row in dgv.Rows)
  8. {
  9. if (row.Selected)
  10. {
  11. return row.Index;
  12. }
  13. }
  14. return 0;
  15. }
复制代码

效果

095616olapod6p8pznna2a.gif

下移一行

  1. private void TaskViewEditHelper_OnDownStep(object sender, EventArgs e)
  2. {
  3. if (this.dataGridView_Task_ViewEdit.SelectedRows == null || this.dataGridView_Task_ViewEdit.SelectedRows.Count == 0)
  4. {
  5. XtraMessageBox.Show("请先选择一行,单击第一列以选中行");
  6. }
  7. else
  8. {
  9. if (this.dataGridView_Task_ViewEdit.SelectedRows[0].Index >= this.dataGridView_Task_ViewEdit.Rows.Count - 1)
  10. {
  11. XtraMessageBox.Show("此行已在底端,不能再下移!");
  12. }
  13. else
  14. {
  15. int selectedRowIndex = GetSelectedRowIndex(this.dataGridView_Task_ViewEdit);
  16. if (selectedRowIndex < dataGridView_Task_ViewEdit.Rows.Count - 1)
  17. {
  18. // 拷贝选中的行
  19. DataGridViewRow newRow = dataGridView_Task_ViewEdit.Rows[selectedRowIndex];
  20. // 删除选中的行
  21. dataGridView_Task_ViewEdit.Rows.Remove(dataGridView_Task_ViewEdit.Rows[selectedRowIndex]);
  22. // 将拷贝的行,插入到选中的下一行位置
  23. dataGridView_Task_ViewEdit.Rows.Insert(selectedRowIndex + 1, newRow);
  24. dataGridView_Task_ViewEdit.ClearSelection();
  25. // 选中最初选中的行
  26. dataGridView_Task_ViewEdit.Rows[selectedRowIndex + 1].Selected = true;
  27. }
  28. }
  29. }
  30. }
复制代码

效果

095617s2df8h928hmhl80m.gif

C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
lesliehuang | 2020-11-2 23:44:49 | 显示全部楼层
lesliehuang | 2020-11-2 23:46:33 | 显示全部楼层
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则