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

article/2025/11/1 1:41:21

场景

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

效果

 


http://chatgpt.dhexx.cn/article/QDPWQtaX.shtml

相关文章

DataGridview获取选中行数

DataGridview获取选中行数 代码&#xff1a; dataGridView1.CurrentRow.Index//获取选中行数使用Messbox.Show()弹窗&#xff1a;

DataGridview动态初始化

DataGridview动态初始化 this.dataGridView1.Rows.Add("参数"&#xff0c;"参数"...");Add&#xff08;&#xff09;方法里面可以根据列数而添加相应个数的参数 例如&#xff1a; 上图的列数有4个&#xff0c;所以如果要将DataGridview控件里面添加…

C# 解决datagridview控件显示大量数据拖拉卡顿问题

问题描述&#xff1a; 由于在使用SQL查询大量的数据并一次显示到dataGridView控件&#xff0c;出现拖拉的时候卡顿。 解决方法&#xff1a; 1.首先分页。 2.其次把显示控件设置双buffer。 解决过程如下&#xff1a; 1.设置dataGridView双buffer代码如下&#xff0c;需要引用…

DataGridView绑定数据库

背景 今天在做C#实验的时候&#xff0c;遇到了一个难题&#xff1a;需要将数据库中的数据在C#的窗体的DataGridView控件中显示出来。当然老师布置这个作业是在之前做了铺垫的&#xff0c;之前做省县区三级查询时&#xff0c;讲过了SampleData和LiteDB的使用&#xff0c;但是我…

Winform实现在DataGridView控件的单元格中添加多个控件

Winform实现在DataGridView控件的单元格中添加多个控件 背景实现思路关键代码完整代码下载 背景 DataGridView控件的列是支持TextBoxColumn、ComboBoxColumn等类型的&#xff0c;就是DataGridView的单元格进入编辑模式的时候就会出现对应的控件&#xff0c;但是有些业务场景是…

C# datagridview 单行文字自动换行

dgv.DefaultCellStyle.WrapMode True dgv.RowTemplate.DefaultCellStyle.WrapMode True dgb.RowsDefaultCellStyle.WrapMode True dgv.AlternatingRowsDefaultCellStyle.WrapMode True 在设计界面里手动设置&#xff0c;一般情况下&#xff0c;设置其中一个WrapMode为true…

c#提高datagridview刷新速度(两种方法计时对比)

两种方法&#xff0c;代码如下&#xff1a; void initDataGridView(){Stopwatch sw new Stopwatch();//Stopwatch提供一组方法和属性&#xff0c;可用于准确地测量运行时间sw.Start();//方法一&#xff1a;一行一行增加到datagridview中for (int i 0; i < 10000; i){int R…

Winform中打印 dataGridView里的内容

最近评论问题比较多&#xff0c;这是几年前得代码了&#xff0c;今天正好有时间我重新整理了下代码把源码Demo发上来给大家看看互相学习。 有问题随时交流。没有积分得私信我发你。 Demo地址&#xff1a;DataGirdView打印.rar-C#文档类资源-CSDN下载 //调用GridPrinter首先添…

C# DataGridView 使用

1、//dataGridView標題居中 dataGridView2.ColumnHeadersDefaultCellStyle.Alignment DataGridViewContentAlignment.MiddleCenter;2、//dataGrideView文本居中 dataGridView2.RowsDefaultCellStyle.Alignment DataGridViewContentAlignment.MiddleCenter;3、//dataGridVie…

C#中DataGridView操作

DataGridView官方介绍 1.DataGridView增加数据 // 先清理&#xff0c;防止数据脏乱 skinDataGridView1.Rows.Clear(); // 获取最新行的索引 int index skinDataGridView1.Rows.Add(); // 添加数据 skinDataGridView1.Rows[index].Cells[0].Value "第index行第一列&quo…

C#Winform的DataGridView控件使用详解1—七种DataGridViewColumn类型使用方法

C#Winform的DataGridView控件使用详解1—七种DataGridViewColumn类型使用方法 DataGirdView控件Column类型DataGridViewButtonColumn列&#xff1a;按钮DataGridViewCheckBoxColumn列&#xff1a;复选框DataGridViewComboBoxColumn列&#xff1a;下拉框DataGridViewImageColumn…

DataGridView简单介绍

在很多软件中都需要查询数据&#xff0c;显示数据&#xff0c;机房收费系统也是非常多的。在这里我们就用到了DataGridView控件。 一&#xff0c;概述&#xff1a; 使用 DataGridView控件&#xff0c;可以显示和编辑来自多种不同类型的数据源的表格数据。它可以通过设置属性直接…

C#Winform的DataGridView控件使用详解2—DataGridView表格样式设置及表格操作

C#Winform的DataGridView控件使用详解2—DataGridView表格样式设置及表格操作 DataGridView表格样式设置DataGridView行序号设置 右键弹出控件表格操作DataGridView新建行DataGridView删除行DataGridView清除内容DataGridView复制DataGridView粘贴 在展示和处理二维数据时&…

C# DataGridView控件的基础应用实例

目录 引言一、界面简介二、初始化三、添加一行数据四、允许修改表格五、复制选择的数据六、复制所有数据七、读一行数据八、读所有数据九、查找名字记录十、删除一行数据十一、删除多行数据十二、清除所有行十三、删除所有列十四、其它&#xff1a;选中单元格十五、最后 引言 …

基于用户的协同过滤个性化音乐推荐系统毕业设计

基于用户的协同过滤个性化音乐推荐系统 摘 要 互联网发展到如今已经完完全全的改变了的生活方式&#xff0c;融入了日常生活&#xff0c;包括交流&#xff0c;出行&#xff0c;消费&#xff0c;娱乐等。与此同时&#xff0c;音乐数据也在与日俱增的变化着。用户在访问一个…

基于深度学习的音乐推荐系统

♚ 作者&#xff1a;沂水寒城&#xff0c;CSDN博客专家&#xff0c;个人研究方向&#xff1a;机器学习、深度学习、NLP、CV Blog: http://yishuihancheng.blog.csdn.net 推荐系统在我们日常生活中发挥着非常重要的作用&#xff0c;相信实际从事过推荐相关的工程项目的人或多或少…

(附源码)计算机毕业设计SSM基于java的音乐推荐系统

&#xff08;附源码&#xff09;计算机毕业设计SSM基于java的音乐推荐系统 项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技…

【计算机毕业设计】Java基于协同过滤算法的音乐推荐系统

毕设帮助、开题指导、源码交流&#xff0c;联系方式见文末。。 音乐检索系统的开发目的是使音乐检索模式转变成信息管理&#xff0c;为音乐检索人员提供方便条件。对音乐检索的实际情况进行调研之后&#xff0c;进行详细的需求分析&#xff0c;对现有的管理模式进行改进&#x…

java基于springboot+vue协同过滤算法的音乐推荐系统

音乐是人类永恒的话题&#xff0c;无论是在古代还是现代人们对音乐都有一种非常的热爱在里面&#xff0c;同时音乐也寄语了人们对美好事物的憧憬&#xff0c;很多时候人们在试听音乐的时候并不能够及时的找到适合自己的音乐&#xff0c;而且当下很多音乐都是收费的&#xff0c;…

[附源码]java毕业设计网易云音乐推荐系统

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…