功能:

一.列相关:

1.追加列,左插列,右插列,

2.删除列

二.行相关:

1.追加行,上插行,下插行

2.删除行,删除所有空行,清空所有数据...

原理:根据对鼠标于 DataGridView 点击区域的判断来 对 点击列 或 点击行 的准确定位,再执行操作...

优点:

1.只需要 CellMouseDown 事件所输出的 DataGridViewCellMouseEventArgs 参数来判断,便可取代 通常情况下对行列操作函数所需要输入的 点击列 或 点击行...
2.具有行列相关操作 某些方面 一定的综合性;

缺点:
1.由于其在某些操作方面的综合性,相比专一针对某个列或某行进行操作的函数肯定稍慢..

注明:

1.所提供的函数可能还没有达到所有情况下的测试,但目前本人还没发现出错的地方...

2.新增列操作函数中有可以改进的地方...[相关代码段有注明]

以下是相关代码:

 public static partial class Dgv
{
#region 点击行列 基本信息 获取 /// <summary>
/// 根据鼠标点击的单元格信息,输出 单元格 的作用分类作用区域
/// 注:由于 dgv 的列名行/行标头 都是有不同分工作用的单元格 Cell 组成,所以,以下判断均为判断 点击的 Cell 所在的 行索引与列索引来判断 所属的分类作用区域
/// 时间: 2021/07/02 20:00:58
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public static EAra _CellMouseDown(object s, DataGridViewCellMouseEventArgs e, out DataGridViewColumn clkDgvC, out DataGridViewRow clkDgvR, out DataGridViewCell clkDgvc)
{
DataGridView dgv = s as DataGridView;
return _CellMouseDown(dgv, e, out clkDgvC, out clkDgvR, out clkDgvc);
}
/// <summary>
/// 根据鼠标点击的单元格信息,输出 单元格 的作用分类作用区域
/// 注:由于 dgv 的列名行/行标头 都是有不同分工作用的单元格 Cell 组成,所以,以下判断均为判断 点击的 Cell 所在的 行索引与列索引来判断 所属的分类作用区域
/// 时间: 2021/07/02 20:00:58
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public static EAra _CellMouseDown(DataGridView dgv, DataGridViewCellMouseEventArgs e, out DataGridViewColumn clkDgvC, out DataGridViewRow clkDgvR, out DataGridViewCell clkDgvc)
{
#region clkDgvC = null;
clkDgvR = null;
clkDgvc = null;
EAra eAra = EAra.non; if (e == null)
eAra = EAra.all;
else
{
#region int iRow = e.RowIndex;
int iCol = e.ColumnIndex; if (iRow < 0)
{
#region if (iCol < 0)
eAra = EAra.all;
else
{
eAra = EAra.col;
clkDgvC = dgv.Columns[iCol];
} #endregion
}
else if (iCol < 0)
{
eAra = EAra.row;
clkDgvR = dgv.Rows[iRow];//.CurrentRow;
}
else
{
eAra = EAra.cel;
clkDgvc = dgv.Rows[iRow].Cells[iCol];//.CurrentCell;
} #endregion
}
return eAra; #endregion
}
/// <summary>
/// 根据鼠标点击的单元格信息,输出 单元格 的作用分类作用区域
/// 注:由于 dgv 的列名行/行标头 都是有不同分工作用的单元格 Cell 组成,所以,以下判断均为判断 点击的 Cell 所在的 行索引与列索引来判断 所属的分类作用区域
/// 时间: 2021/07/02 20:00:58
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public static EAra _CellMouseDown(DataGridViewCellMouseEventArgs e)
{
#region EAra eAra = EAra.non;
if (e == null)
eAra = EAra.all;
else
{
#region int iRow = e.RowIndex;
int iCol = e.ColumnIndex; if (iRow < 0)
{
#region if (iCol < 0)
eAra = EAra.all;
else
eAra = EAra.col; #endregion
}
else if (iCol < 0)
eAra = EAra.row;
else
eAra = EAra.cel; #endregion
} return eAra; #endregion
}
/// <summary>
/// 根据 _CellMouseDown 判断点击的区域, 获取可能与点击操作相关的列 [如:点击单元格对应的列,直击列名列,反之为 null ]
/// 时间: 2021/07/02 20:00:58
/// </summary>
/// <param name="dgv"></param>
/// <param name="e"></param>
/// <param name="clkDgvC"></param>
public static void ClkDgvC(DataGridView dgv, DataGridViewCellMouseEventArgs e, out DataGridViewColumn clkDgvC)
{
#region clkDgvC = null;
DataGridViewRow clkDgvR = null;
DataGridViewCell clkDgvc = null; EAra eAra = _CellMouseDown(dgv, e, out clkDgvC, out clkDgvR, out clkDgvc);
if (eAra == EAra.all || eAra == EAra.non)
return; if (clkDgvR != null)
return; if (clkDgvC == null)
{
if (clkDgvc == null)
return; clkDgvC = clkDgvc.OwningColumn;
} #endregion
} #endregion /// <summary>
/// [综合情况] 综合处理
/// 时间: 2021/07/02 23:45:58
/// </summary>
public static class Col2
{
#region #region [综合情况] 新增列 /// <summary>
/// [综合情况] 新增列
/// </summary>
/// <param name="dgv"></param>
/// <param name="insCelTyp"></param>
/// <param name="colNam"></param>
/// <param name="insRit"></param>
/// <param name="e"></param>
public static void New(DataGridView dgv, DataGridViewCell insCelTyp, string colNam, bool insRit, ref DataGridViewCellMouseEventArgs e)
{
#region #region DataGridViewColumn clkDgvC = null;
ClkDgvC(dgv, e, out clkDgvC); int cnt = dgv.ColumnCount;
cnt++;
if (colNam == "")
colNam = "F" + cnt; int dspIdx = -1;
if (clkDgvC == null)
dspIdx = cnt;
else
{
dspIdx = clkDgvC.DisplayIndex;
if (insRit)
dspIdx++;
} #endregion #region object obj = dgv.DataSource;
if (obj == null)
{
#region if (insCelTyp == null)
insCelTyp = Col.ETyp.cTxt; DataGridViewColumn dgvC = new DataGridViewColumn(insCelTyp);
dgvC.SortMode = DataGridViewColumnSortMode.Automatic;
dgvC.HeaderText = colNam;
dgv.Columns.Add(dgvC);
dgvC.DisplayIndex = dspIdx; #endregion
}
else
{
#region string typ = obj.GetType().Name;
if (typ == EDat.DataTable + "")
{
#region #region 操作 DataTable 对象 DataGridViewColumn dgvC; #region 以下包含代码可能有别的解决方法,此不临时替代 #region 记录加列前的列的 显序 int[] dspIdxS = new int[cnt];
for (int i = 0; i < cnt - 1; i++)
{
dgvC = dgv.Columns[i];
dspIdxS[i] = dgvC.DisplayIndex;
} #endregion #region 注:以上代码可能会自动打乱原列 显序 DataTable dt = obj as DataTable;
DT.Col.Add(dt, colNam); #endregion #region 还原加列前的列的 显序 for (int i = 0; i < cnt - 1; i++)
{
dgvC = dgv.Columns[i];
dgvC.DisplayIndex = dspIdxS[i];
} #endregion #endregion #endregion if (clkDgvC != null)
{
#region cnt--;
dgvC = dgv.Columns[cnt];
dgvC.SortMode = DataGridViewColumnSortMode.Automatic;
dgvC.DisplayIndex = dspIdx; #endregion
} #endregion
} #endregion
} #endregion #endregion
}
/// <summary>
/// [综合情况] 左插列
/// </summary>
/// <param name="dgv"></param>
/// <param name="insCelTyp"></param>
/// <param name="colNam"></param>
/// <param name="e"></param>
public static void Ins(DataGridView dgv, DataGridViewCell insCelTyp, string colNam, DataGridViewCellMouseEventArgs e)
{
New(dgv, insCelTyp, colNam, false, ref e);
}
/// <summary>
/// [综合情况] 右插列
/// </summary>
/// <param name="dgv"></param>
/// <param name="insCelTyp"></param>
/// <param name="colNam"></param>
/// <param name="e"></param>
public static void Ins_(DataGridView dgv, DataGridViewCell insCelTyp, string colNam, DataGridViewCellMouseEventArgs e)
{
New(dgv, insCelTyp, colNam, true, ref e);
} /// <summary>
/// [综合情况] 新增列
/// </summary>
/// <param name="dgv"></param>
/// <param name="colNam"></param>
/// <param name="insRit"></param>
/// <param name="e"></param>
public static void New(DataGridView dgv, string colNam, bool insRit,ref DataGridViewCellMouseEventArgs e)
{
DataGridViewCell insCelTyp = Col.ETyp.cTxt;
New(dgv, insCelTyp, colNam, insRit, ref e);
}
/// <summary>
/// [综合情况] 左插列
/// </summary>
/// <param name="dgv"></param>
/// <param name="colNam"></param>
/// <param name="insRit"></param>
/// <param name="e"></param>
public static void Ins(DataGridView dgv, string colNam, bool insRit, DataGridViewCellMouseEventArgs e)
{
New(dgv, colNam, false,ref e);
}
/// <summary>
/// [综合情况] 右插列
/// </summary>
/// <param name="dgv"></param>
/// <param name="colNam"></param>
/// <param name="insRit"></param>
/// <param name="e"></param>
public static void Ins_(DataGridView dgv, string colNam, bool insRit, DataGridViewCellMouseEventArgs e)
{
New(dgv, colNam, true, ref e);
} #endregion #region [综合情况] 删除列 /// <summary>
/// [综合情况] 删除列
/// </summary>
/// <param name="dgv"></param>
/// <param name="e"></param>
public static void Del(DataGridView dgv, DataGridViewCellMouseEventArgs e)
{
#region DataGridViewColumn clkDgvC = null;
ClkDgvC(dgv, e, out clkDgvC); if (clkDgvC == null)
return; object obj = dgv.DataSource;
if (obj == null)
dgv.Columns.Remove(clkDgvC);
else
{
string typ = obj.GetType().Name;
if (typ == EDat.DataTable + "")
{
int colIdx = clkDgvC.Index;
DataTable dt = obj as DataTable;
dt.Columns.RemoveAt(colIdx);
}
} #endregion
} #endregion #endregion
} /// <summary>
/// [综合情况] 综合处理
/// 时间: 2021/07/02 23:45:58
/// </summary>
public static class Row2
{
#region #region [综合情况] 新增行 /// <summary>
/// [综合情况] 新增行
/// </summary>
/// <param name="dgv"></param>
/// <param name="insDwn"></param>
/// <param name="e"></param>
public static void New(DataGridView dgv, bool insDwn, ref DataGridViewCellMouseEventArgs e)
{
#region #region DataGridViewColumn clkDgvC;
DataGridViewRow clkDgvR;
DataGridViewCell clkDgvc;
_CellMouseDown(dgv, e, out clkDgvC, out clkDgvR, out clkDgvc); #endregion #region int row = dgv.Rows.Count;
bool bol = dgv.AllowUserToAddRows;
if (bol)
row--; int insIdx = -1;
if (clkDgvR == null)
insIdx = row;// --row;
else
{
insIdx = clkDgvR.Index;
if (insDwn)
insIdx++;
else
{
#region 此码作用于 能在 点击行处 连续 添加 MouseEventArgs mse = new MouseEventArgs(MouseButtons.Left, 1, 1, 1, 0);
e = new DataGridViewCellMouseEventArgs(e.ColumnIndex, insIdx + 1, e.Location.X, e.Location.Y, mse); #endregion
}
} #endregion #region object obj = dgv.DataSource;
if (obj == null)
{
#region if (insIdx < row)
dgv.Rows.Insert(insIdx, 1);
else
dgv.Rows.Add(); #endregion
}
else
{
#region string typ = obj.GetType().Name;
if (typ == EDat.DataTable + "")
{
#region DataTable dt = obj as DataTable;
if (insIdx < row)
{
DataRow dr = dt.NewRow();
dt.Rows.InsertAt(dr, insIdx);
}
else
dt.Rows.Add(); #endregion
} #endregion
} #endregion #endregion
}
/// <summary>
/// [综合情况] 在当前行处插入新行
/// </summary>
/// <param name="dgv"></param>
/// <param name="e"></param>
public static void Ins(DataGridView dgv, ref DataGridViewCellMouseEventArgs e)
{
New(dgv, false,ref e);
}
/// <summary>
/// [综合情况] 在当前行下面插入新行
/// </summary>
/// <param name="dgv"></param>
/// <param name="e"></param>
public static void Ins_(DataGridView dgv,ref DataGridViewCellMouseEventArgs e)
{
New(dgv, true,ref e);
} #endregion #region [综合情况] 删除行 public static void Del(DataGridView dgv,ref DataGridViewCellMouseEventArgs e)
{
#region #region DataGridViewColumn clkDgvC;
DataGridViewRow clkDgvR;
DataGridViewCell clkDgvc;
_CellMouseDown(dgv, e, out clkDgvC, out clkDgvR, out clkDgvc); if (clkDgvR == null)
return; #endregion #region int rowIdx = clkDgvR.Index;
bool bol = dgv.AllowUserToAddRows;
int cnt = dgv.Rows.Count;
if (bol)
{
cnt--;
if (rowIdx == cnt)
return;
} #endregion #region object obj = dgv.DataSource;
if (obj == null)
dgv.Rows.RemoveAt(rowIdx);
else
{
#region string typ = obj.GetType().Name;
if (typ == EDat.DataTable + "")
{
DataTable dt = obj as DataTable;
dt.Rows.RemoveAt(rowIdx);
} #endregion
} cnt--;
if (cnt < 1)
{
MouseEventArgs mse = new MouseEventArgs(MouseButtons.Left, 1, 1, 1, 0);
e = new DataGridViewCellMouseEventArgs(e.ColumnIndex, -1, e.Location.X, e.Location.Y, mse);
} #endregion #endregion
} #endregion #region [综合情况] 删除所有空行 /// <summary>
/// [综合情况] 删除所有空行
/// </summary>
/// <param name="dgv"></param>
public static void Trm(DataGridView dgv)
{
#region int row = dgv.Rows.Count;
if (dgv.AllowUserToAddRows)
row--; if (row < 1)
return; row--;
int j;
int col = dgv.Columns.Count; #endregion #region string vlu;
object obj = dgv.DataSource;
if(obj==null)
{
#region DataGridViewRow dgvR;
DataGridViewCell dgvc;
for (int i = row; i > -1; i--)
{
dgvR = dgv.Rows[i];
for (j = 0; j < col; j++)
{
dgvc = dgvR.Cells[j];
vlu = dgvc.Value.ToString();
if (vlu != "")// dgvc.Value != null)
break;
}
if (j < col)
continue; dgvR = null;
dgv.Rows.RemoveAt(i);
} #endregion
}
else
{
string typ = obj.GetType().Name;
if(typ==EDat.DataTable+"")
{
#region DataTable dt = obj as DataTable;
DataRow dr;
for (int i = row; i > -1; i--)
{
dr = dt.Rows[i];
for (j = 0; j < col; j++)
{
vlu = dr[j] + "";
if (vlu != "")
break;
}
if (j < col)
continue; dr = null;
dt.Rows.RemoveAt(i);
} #endregion
}
} #endregion
} #endregion #region [综合情况] 清空所有行 /// <summary>
/// [综合情况] 清空所有行
/// </summary>
/// <param name="dgv"></param>
public static void Clr(DataGridView dgv)
{
#region int row = dgv.Rows.Count;
if (dgv.AllowUserToAddRows)
row--; if (row < 1)
return; row--; #endregion #region object obj = dgv.DataSource;
if (obj == null)
dgv.Rows.Clear();
else
{
string typ = obj.GetType().Name;
if (typ == EDat.DataTable + "")
{
#region DataTable dt = obj as DataTable;
dt.Rows.Clear(); #endregion
}
} #endregion
} #endregion #region [综合情况] 清空所有数据 /// <summary>
/// [综合情况] 清空所有数据
/// </summary>
/// <param name="dgv"></param>
public static void Nul(DataGridView dgv)
{
#region int row = dgv.Rows.Count;
if (dgv.AllowUserToAddRows)
row--; if (row < 1)
return; row--;
int j;
int col = dgv.Columns.Count; #endregion #region object obj = dgv.DataSource;
if (obj == null)
{
#region DataGridViewRow dgvR;
DataGridViewCell dgvc;
for (int i = row; i > -1; i--)
{
dgvR = dgv.Rows[i];
for (j = 0; j < col; j++)
{
dgvc = dgvR.Cells[j];
if (dgvc.Value == null)
continue; dgvc.Value = "";
}
} #endregion
}
else
{
string typ = obj.GetType().Name;
if (typ == EDat.DataTable + "")
{
#region DataTable dt = obj as DataTable;
DataRow dr;
for (int i = row; i > -1; i--)
{
dr = dt.Rows[i];
for (j = 0; j < col; j++)
{
obj = dr[j];
if (obj == null)
continue; dr[j] = "";
}
} #endregion
}
} #endregion
} #endregion #endregion
}
}

交流 QQ : 2412366909@qq.com
手机号码:177-7499-4428

C# DataGridView 新增列 新增行 操作函数 - [ 自律相互分享,共促一起进步 - 社会的正常运维就这么简单,何以权,何以钱...- 张光荣2010年谈社会改正提出的正能量]的更多相关文章

  1. DataGridView控件的各种操作总结

    一.单元格内容的操作 *****// 取得当前单元格内容 Console.WriteLine(DataGridView1.CurrentCell.Value); // 取得当前单元格的列 Index ...

  2. c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)

    一.单元格内容的操作 *****// 取得当前单元格内容 Console.WriteLine(DataGridView1.CurrentCell.Value); // 取得当前单元格的列 Index ...

  3. 转:c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)

    一.单元格内容的操作 *****// 取得当前单元格内容 Console.WriteLine(DataGridView1.CurrentCell.Value); // 取得当前单元格的列 Index  ...

  4. (转)实现DataList的分页 新增列

    前几天在做网上商城,要展示商品信息(有图片,有文字),DataView虽然可以分页,但它的缺点是不能自定义显示格式.而DataList解决了它的缺点,但DataList本身却不能分页.很是头痛,于是在 ...

  5. IDEA04 工具窗口管理、各种跳转、高效定位、行操作、列操作、live template、postfix、alt enter、重构、git使用

    1 工具窗口管理 所有的窗口都是在view -> tools windows 下面的,这些窗口可以放在IDEA的上下左右各个位置:右键某个窗口后选择move to 即可进行位置调整 2 跳转 2 ...

  6. 在 Bootstraptable 插件基础上新增可编辑行

    http://www.tuicool.com/articles/YbEVv2v 为什么调用 bootstraptable 原生方法会有问题 首先我必须肯定, bootstraptable 是一款很强大 ...

  7. 使用 PIVOT 和 UNPIVOT 行转列 列转行 报表统计 函数

    官方文档:http://technet.microsoft.com/zh-cn/library/ms177410(v=SQL.105).aspx 可以使用 PIVOT 和 UNPIVOT 关系运算符将 ...

  8. Oracle行转列(使用pivot函数)

    在日常使用中,经常遇到这样的情况,需要将数据库中行转化成列显示,如 转化为 这个时候,我们就需要使用pivot函数 百度后,参考网址http://www.2cto.com/database/20150 ...

  9. DB2行转列、列转行等操作

    DB2 行转列 ----start 在网上看到这样一个问题:(问题地址:http://www.mydb2.cn/bbs/read.php?tid=1297&page=e&#a) 班级  ...

  10. 大数据学习day28-----hive03------1. null值处理,子串,拼接,类型转换 2.行转列,列转行 3. 窗口函数(over,lead,lag等函数) 4.rank(行号函数)5. json解析函数 6.jdbc连接hive,企业级调优

    1. null值处理,子串,拼接,类型转换 (1) 空字段赋值(null值处理) 当表中的某个字段为null时,比如奖金,当你要统计一个人的总工资时,字段为null的值就无法处理,这个时候就可以使用N ...

随机推荐

  1. 8. semahpore原理

    一.上游服务比下游服务抗压能力应该更强一些,因为直接面对的是前端.Semphore控制访问特定资源的线程数目.实际场景可用于限流.在hystrix里面用了. 另:ReadWriteLock的作用是什么 ...

  2. NC16644【字符串的展开】

    正确代码: #include <iostream>#include <algorithm>using namespace std;bool IsSame(char a, cha ...

  3. Vulnhub 靶机 CONTAINME: 1

    Vulnhub 靶机 CONTAINME: 1 前期准备: 靶机地址:https://www.vulnhub.com/entry/containme-1,729/ kali地址:192.168.147 ...

  4. 【C学习笔记】day2-3 求10 个整数中最大值

    #include <stdio.h>#define n 10 int main() { int max=0; int a[n] = {12,15,16,546,165,654,612,23 ...

  5. node.js 新手快速入门

    我当初学的时候,是在大大们的指导下开始学习的,用了3天搞定大大们给的任务.下面我就把这个经历分享出来,让大家借鉴一下.欢迎吐槽~~ 任务如下: 根据Node js 开发入门教程第五章的一个使用node ...

  6. 3927Circular Sequence 思维题(求环形最大子列和)

    Given a sequence with n elements, if the last element is also adjacent to the first element of the s ...

  7. python3+selenium+BeautifulReport生成自动化测试报告

    https://www.jianshu.com/p/3d2c0e092ffb 自动化测试,最重要的还是测试报告,下面就教大家使用BeautifulReport生成自动化测试报告GitHub:https ...

  8. 本地jar包怎么导入到maven仓库中?

    1.找到你所需要的jar包 2.打开cmd找到jar包的文件夹下 3.输入安装命令实例命令 1 安装指定文件到本地仓库命令:mvn install:install-file 2 -DgroupId=& ...

  9. std::unique_ptr release的使用

    在c++中,动态内存管理是通过new/delete 运算符来进行的.由于确保在正确的时间释放内存是很困难的,为了避免内存泄漏,更加容易,安全地使用动态内存,C++11标准库提供了两种智能指针类型来管理 ...

  10. Delphi书籍大全【阿里云盘】

    「marco cantu的Object Pascal Handbook」等文件 https://www.aliyundrive.com/s/sJtUo8ziUpV 提取码: 5tp6点击链接保存,或者 ...