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