winform DataGridView-----1
winform 中的列表展示一般是ListView控件和DataGridView控件。这次就简单的介绍一下DataGridView,不得不说winform的列表展示,如果自己不去润色和扩展,原始的是真的丑。但是说实话
很多大佬都是自定义控件,人家做的效果是相当的棒。我是初学者,可没有这本是。所以为了让列表展示稍微看的过去,我只能稍微润色一下了。哈哈哈哈
DataGridView简单封装:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | /// <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; } }<br> |
颜色转换:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /// <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); } } } |
枚举:
1 2 3 4 5 6 | public enum ColType { 文本, 图标, 按钮 } |
Model:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | //设置行表头的颜色 //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" ); } |
效果:
.Net Core
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在外漂泊的这几年总结和感悟,展望未来
· 博客园 & 1Panel 联合终身会员上线
· 支付宝事故这事儿,凭什么又是程序员背锅?有没有可能是这样的...
· https证书一键自动续期,帮你解放90天限制
· 在 ASP.NET Core WebAPI如何实现版本控制?
2021-03-18 U3D PC端桌面应用程序远程升级