首页 > 其他分享 >winform DataGridView数据列表-----1

winform DataGridView数据列表-----1

时间:2023-03-18 20:34:33浏览次数:43  
标签:gridView void 表头 DataGridView static ----- public winform

 

     winform 中的列表展示一般是ListView控件和DataGridView控件。这次就简单的介绍一下DataGridView,不得不说winform的列表展示,如果自己不去润色和扩展,原始的是真的丑。但是说实话

很多大佬都是自定义控件,人家做的效果是相当的棒。我是初学者,可没有这本是。所以为了让列表展示稍微看的过去,我只能稍微润色一下了。哈哈哈哈

 

      DataGridView简单封装:

   /// <summary>
    /// dataGridView设置
    /// </summary>
    public static class SetDataGridView
    {
      
        /// <summary>
        /// 表头设置
        /// </summary>
        /// <param name="gridView"></param>
        /// <param name="dicTitle"></param>t
        public static void SetHeader(this DataGridView gridView, List<dataGridViewModel> viewModel)
        {
            foreach (var item in viewModel)
            {
                switch (item.colType)
                {
                    case ColType.文本:
                        gridView.Columns.Add(item.colName, item.colTitle);
                        break;
                    case ColType.图标:
                        gridView.SetImage();
                        break;
                    case ColType.按钮:
                        gridView.SetButton(item.TitleData);
                        break;
                    default:
                        break;
                }
            }
        }
        /// <summary>
        /// 对某些列进行列宽设置
        /// </summary>
        /// <param name="gridView"></param>
        /// <param name="colWidth"></param>
        public static void SetColWidth(this DataGridView gridView, Dictionary<int, int> colWidth)
        {
            foreach (int key in colWidth.Keys)
            {
                gridView.Columns[key].Width = colWidth[key];
            }
        }
        /// <summary>
        /// 设置隐藏某一列
        /// </summary>
        /// <param name="gridView"></param>
        /// <param name="colVisible"></param>
        public static void SetColVisible(this DataGridView gridView, string[] colVisible)
        {
            for (int i = 0; i < colVisible.Length; i++)
            {

                gridView.Columns[colVisible[i]].Visible = false;
            }

        }
        /// <summary>
        /// 列表添加操作按钮
        /// </summary>
        /// <param name="gridView"></param>
        public static void SetButton(this DataGridView gridView, Title title)
        {
            if (title == null)
            {
                return;
            }
          
            DataGridViewButtonColumn select = new DataGridViewButtonColumn
            {
                Name = title.headTitle,
                Text = title.Text,
                UseColumnTextForButtonValue = true,
                DataPropertyName = "operate",
                FillWeight = 5,
                HeaderText = title.headText
               
            };
           
            select.FlatStyle = FlatStyle.Flat;
            select.Width = 20;

            select.DefaultCellStyle.ForeColor = string.IsNullOrEmpty(title.color) ? Color.Black : SetColor.colorHx16toRGB(title.color);
            gridView.Columns.Add(select);
        }
        /// <summary>
        /// 列表添加图片
        /// </summary>
        /// <param name="gridView"></param>
        public static void SetImage(this DataGridView gridView)
        {
            DataGridViewImageColumn Imange = new DataGridViewImageColumn();
            Imange.ImageLayout = DataGridViewImageCellLayout.Zoom;
            // Imange.HeaderText = "图标";
            gridView.Columns.Add(Imange);
        }
        /// <summary>
        /// 表头颜色设置
        /// </summary>
        public static void SetHeadColor(this DataGridView gridView, string color)
        {
            //设置行表头的颜色
            gridView.EnableHeadersVisualStyles = false;
            gridView.ColumnHeadersDefaultCellStyle.BackColor = SetColor.colorHx16toRGB(color);

        }
        /// <summary>
        /// 表头颜色设置
        /// </summary>
        public static void SetHeadColor(this DataGridView gridView, int R, int G, int B)
        {
            //设置行表头的颜色
            gridView.EnableHeadersVisualStyles = false;
            gridView.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(R, G, B);

        }
        /// <summary>
        /// 设置表头文字颜色
        /// </summary>
        /// <param name="gridView"></param>
        /// <param name="fontcolor"></param>
        public static void SetHeadFontColor(this DataGridView gridView, string fontcolor)
        {
            //表头文字颜色
            gridView.ColumnHeadersDefaultCellStyle.ForeColor = SetColor.colorHx16toRGB(fontcolor);
        }
        /// <summary>
        /// 设置表头文字颜色
        /// </summary>
        /// <param name="gridView"></param>
        /// <param name="fontcolor"></param>
        public static void SetHeadFontColor(this DataGridView gridView, int R, int G, int B)
        {
            //表头文字颜色
            gridView.ColumnHeadersDefaultCellStyle.ForeColor = Color.FromArgb(R, G, B);
        }
        /// <summary>
        /// 设置文字类型
        /// </summary>
        /// <param name="gridView"></param>
        /// <param name="fontType"></param>
        public static void SetHeadFontType(this DataGridView gridView, Font fontType)
        {
            //行表头文字类型
            gridView.ColumnHeadersDefaultCellStyle.Font = fontType;
        }
        /// <summary>
        /// 设置表头行高0
        /// </summary>
        /// <param name="gridView"></param>
        /// <param name="rowHeight"></param>
        public static void SetHeadRowHeight(this DataGridView gridView, int rowHeight)
        {
            //改变标题的行高
            gridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
            gridView.ColumnHeadersHeight = rowHeight;
        }
        /// <summary>
        /// 居中标题
        /// </summary>
        /// <param name="gridView"></param>
        public static void SetHeadCenter(this DataGridView gridView)
        {
            gridView.AutoSize = false;
            //标题居中
            gridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }
        /// <summary>
        /// 去除第一列空列
        /// </summary>
        /// <param name="gridView"></param>
        public static void SetRowHeadersVisible(this DataGridView gridView)
        {
            gridView.RowHeadersVisible = false;

        }
        /// <summary>
        /// 去除最后一行空行
        /// </summary>
        /// <param name="gridView"></param>
        public static void SetAllowUserToAddRows(this DataGridView gridView)
        {
            gridView.AllowUserToAddRows = false;
        }
        public static void SetCellRowFull(this DataGridView gridView)
        {
            gridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }
    }

  颜色转换:

 /// <summary>
    /// 颜色转换
    /// </summary>
    public class SetColor
    {
        public static Color colorHx16toRGB(string strHxColor)
        {
            try
            {
                if (strHxColor.Length == 0)
                {//如果为空
                    return Color.FromArgb(0, 0, 0);//设为黑色
                }
                else
                {//转换颜色
                    return Color.FromArgb(System.Int32.Parse(strHxColor.Substring(1, 2), System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse(strHxColor.Substring(3, 2), System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse(strHxColor.Substring(5, 2), System.Globalization.NumberStyles.AllowHexSpecifier));
                }
            }
            catch
            {//设为黑色
                return Color.FromArgb(0, 0, 0);
            }
        }
    }

枚举:

 

 public enum ColType
    {
        文本,
        图标,
        按钮
    }

  

Model:

 public class dataGridViewModel
    {
        public string colName { get; set; }
        public string colTitle { get; set; }
        public ColType colType { get; set; }
        public Title TitleData { get; set; }

    }
    public class Title
    {
        public string headTitle { get; set; }
        public string Text { get; set; }
        public string headText { get; set; }
        public string color { get; set; }
    }

 c测试:

 //设置行表头的颜色
            //dataGridView_Mark.SetHeadColor("#F5F5F5");
            dataGridView1.SetHeadColor(250, 250, 250);
            //表头文字颜色
            //  dataGridView_File2.SetHeadFontColor("#F5F5F5");
            dataGridView1.SetHeadFontColor(95, 95, 95);
            //行表头文字类型
            dataGridView1.SetHeadFontType(new Font("楷体", 8, FontStyle.Bold));
            //改变标题的行高
            dataGridView1.SetHeadRowHeight(30);
            //去除第一列空列
            dataGridView1.SetRowHeadersVisible();
            //行占满
            dataGridView1.SetCellRowFull();
            //去除最后一行空行
            dataGridView1.SetAllowUserToAddRows();
            //标题居中
            dataGridView1.SetHeadCenter();


            List<dataGridViewModel> viewModel = new List<dataGridViewModel>()
            {
                new dataGridViewModel(){colName="title",colTitle="标题",colType=ColType.文本},
                new dataGridViewModel(){colType=ColType.图标},
                new dataGridViewModel(){colName="title1",colTitle="标题",colType=ColType.文本},
                new dataGridViewModel(){colName="title2",colTitle="标题",colType=ColType.文本},
                new dataGridViewModel(){colName="title3",colTitle="标题",colType=ColType.文本},
                new dataGridViewModel(){colName="title4",colTitle="标题",colType=ColType.文本},
                new dataGridViewModel(){colName="title5",colTitle="标题",colType=ColType.文本},
                new dataGridViewModel(){ colType = ColType.按钮,TitleData=new Title(){ headTitle="btnfile4",Text="按钮",headText="操作" } },

            };
            dataGridView1.SetHeader(viewModel);
            //设置某列隐藏
            // dataGridView_ReportList.SetColVisible(new string[] { "PKID", "customer" });
            Dictionary<int, int> colWidth = new Dictionary<int, int>()
            {
                [1] = 30,
                [7] = 50,
            };
            ////设置列宽
            dataGridView1.SetColWidth(colWidth);
            Image image = Properties.Resources.excel;
            for (int i = 0; i < 20; i++)
            {
                dataGridView1.Rows.Add("123", image, "123",  "123", "123", "123", "123");

            }

  效果:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签:gridView,void,表头,DataGridView,static,-----,public,winform
From: https://www.cnblogs.com/zpy1993-09/p/17231670.html

相关文章

  • SpringCloud-搭建eurekaServer
    Eureka介绍整体介绍背景:在传统应用中,组件之间的调用,通过有规范的约束的接口来实现,从而实现不同模块间良好的协作。但是被拆分成微服务后,每个微服务实例的网络地址都可......
  • java的流程控制-break continue
    breakcontinuebreak:在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。(break也在switch语句中使用) continue:在......
  • YOLO精讲------YOLOV1
    CV小白说YOLOV1题外话:目标检测是什么?它是在图像中对一类或多类感兴趣的目标进行查找和分类,确定它们的类别和位置。由于各类物体有不同的外观、形状和姿态,加上成像时各......
  • HTML--表格
    <!DOCTYPEhtml><html><head><title>表格1</title><linkrel="stylesheet"href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.m......
  • 旁挂式三层组网-隧道转发模式web配置
    拓扑图相关命令1.AR12.AR23.LSW14.LSW25.AC16.Cloudweb配置在ac1上配置与cloud相关网段的vlan1,同时开启HTTP服务在浏览器上登入https://网段......
  • Vue2入门之超详细教程三-初识模板语法
    1、简介模板语法就是按照固定的模板去书写代码,分为插值语法和指令语法。差值语法:功能:用于解析标签体内容写法:{{xxxx}},xxx是js表达式,且可以读取......
  • 力扣262(MySQL)-行程和用户(困难)
    题目:表:Trips 表:Users取消率的计算方式如下:(被司机或乘客取消的非禁止用户生成的订单数量)/(非禁止用户生成的订单总数)。写一段SQL语句查出 "2013-10-01" 至......
  • webpack原理(2):ES6 module在Webpack中如何Tree-shaking构建
    Tree-shaking最早由​​打包工具Rollup​​ 提出DCE作用于模块内(webpack的DCE通过UglifyJS完成),而Tree-shaking则是在打包的时候通过模块之间的信息打包必须的代......
  • ua-parser-js 获取浏览器信息
    这个依赖能精准识别当前访问的浏览器相关信息,亲测好用 点击访问github链接 使用示例letparser=newUAParser();console.log(parser);console.log(parser.getO......
  • YOLO详解------YOLOV1
    CV小白说YOLOV1题外话:目标检测是什么?它是在图像中对一类或多类感兴趣的目标进行查找和分类,确定它们的类别和位置。由于各类物体有不同的外观、形状和姿态,加上成像时各......