zoukankan      html  css  js  c++  java
  • winform DataGridView设置行高 列宽 设置内容格式 验证输入单元格的内容是否正确

    1、设置行高 列宽 设置内容格式 验证输入单元格的内容是否正确 

      private void Form1_Load(object sender, EventArgs e)
            {
                //设置网格颜色
                dataGridView1.GridColor = Color.Red;
                List<Student> stu = new List<Student>();
                Student s1=new Student();       
                s1.Name="小明";
                s1.age=23;
                stu.Add(s1);
                Student s2 = new Student();
                s2.Name = "老张";
                s2.age = 21;
                stu.Add(s2);
                Student s11 = new Student();
                s11.Name = "小李";
                s11.age = 30;
                stu.Add(s11);
                Student s22 = new Student();
                s22.Name = "云峰";
                s22.age = 20;
                stu.Add(s22);
                //绑定数据源
                dataGridView1.DataSource=stu;
                //设置网格中字体的样式
                dataGridView1.DefaultCellStyle.Font = new Font("黑体", 15);
                //设置列宽
                dataGridView1.Columns[1].Width = 170;
                //设置内容格式
                dataGridView1.DefaultCellStyle.Format = "c";
                //设置行高
                dataGridView1.Rows[0].Height = 70;
                //设置对齐方式
                dataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight;
               //是否换行显示过长的内容
                dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True ;
            }
            //单元格验证时事件  可以验证指定列输入的内容是否符合要求
            private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
            {
                if (e.ColumnIndex == 1) //验指定列
                {
                    int result = 0;
                    if (!int.TryParse(e.FormattedValue.ToString(), out result))
                    {
                        dataGridView1.Rows[e.RowIndex].ErrorText = "内容必须为整数类型"; //提示错误信息
                        e.Cancel = true;
                    }
                }
            }
        }
        class Student
        {
            public string Name { get; set; }
            public int age { get; set; }
        }
  • 相关阅读:
    (二十五)Struts2 Tiles集成
    (二十四)Struts2 Spring集成
    etcd 和 redis的使用场景
    react v16.12 源码阅读环境搭建
    gmail邮箱怎么批量删除邮件
    动态创建的元素怎么做动画
    Window 添加定时任务
    commons-pool2-中的一些配置
    Activiti 5.18 流程Model 转成 流程BPMN文件
    Activiti 使用自己的身份认证服务
  • 原文地址:https://www.cnblogs.com/zyadmin/p/8135984.html
Copyright © 2011-2022 走看看