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

功能:

一.列相关:

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 新增列 新增行 操作函数 - [ 自律相互分享,共促一起进步 - 社会的正常运维就这么简单,何以权,何以钱...- 张光荣说的正能量]

上一篇:LeetCode.二叉树的锯齿形层序遍历


下一篇:linux服务器安装svn超详细介绍