欢迎来到 IT实训基地-南通科迅教育
咨询电话:0513-81107100
Datagridview外观设置
2018/3/26
科迅教育
1141
南通零基础Java培训机构效果怎么样

今天我们来介绍winform中datagridview的一些设置外观方面的,首先先看效果图

图一:

1.png

可以设置表头的颜色,行和行之间交替变色,并且鼠标滑过之后可以改变颜色。因为是外观,所以表格的数据显示就完全弄的指定数据源的方式,这里就没写代码了。

页面控件设计如图:

图二:

2.png

页面和控件如图所示,先贴出总体外观设计代码:

this.dataGridView1.AllowUserToAddRows = false;

            this.dataGridView1.AllowUserToDeleteRows = false;

            dataGridViewCellStyle1.BackColor = System.Drawing.Color.LightCyan;

            this.dataGridView1.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;

            this.dataGridView1.AutoGenerateColumns = false;

            this.dataGridView1.BackgroundColor = System.Drawing.Color.White;

            this.dataGridView1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

            this.dataGridView1.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;

            dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;

            dataGridViewCellStyle2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(211)))), ((int)(((byte)(223)))), ((int)(((byte)(240)))));

            dataGridViewCellStyle2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

            dataGridViewCellStyle2.ForeColor = System.Drawing.Color.Navy;

            dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;

            dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;

            this.dataGridView1.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2;

            this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {

            this.sidDataGridViewTextBoxColumn,

            this.nameDataGridViewTextBoxColumn,

            this.ageDataGridViewTextBoxColumn,

            this.sexDataGridViewTextBoxColumn,

            this.addressDataGridViewTextBoxColumn,

            this.emailDataGridViewTextBoxColumn,

            this.telDataGridViewTextBoxColumn,

            this.tidDataGridViewTextBoxColumn,

            this.birthDataGridViewTextBoxColumn});

            this.dataGridView1.DataSource = this.peopleBindingSource;

            this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;

            this.dataGridView1.EnableHeadersVisualStyles = false;

            this.dataGridView1.GridColor = System.Drawing.SystemColors.GradientInactiveCaption;

            this.dataGridView1.Location = new System.Drawing.Point(0, 0);

            this.dataGridView1.Name = "dataGridView1";

            this.dataGridView1.ReadOnly = true;

            this.dataGridView1.RowHeadersVisible = false;

            this.dataGridView1.RowTemplate.Height = 23;

            this.dataGridView1.RowTemplate.ReadOnly = true;

            this.dataGridView1.RowTemplate.Resizable = System.Windows.Forms.DataGridViewTriState.False;

            this.dataGridView1.Size = new System.Drawing.Size(1045, 690);

            this.dataGridView1.TabIndex = 0;

            this.dataGridView1.CellMouseEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellMouseEnter);

            this.dataGridView1.CellMouseLeave += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellMouseLeave);

            this.dataGridView1.RowStateChanged += new System.Windows.Forms.DataGridViewRowStateChangedEventHandler(this.dataGridView1_RowStateChanged);

事件代码:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace DGVdemo

{

    public partial class frmGrid : Form

    {

        public frmGrid()

        {

            InitializeComponent();

        }

 

        private void frmGrid_Load(object sender, EventArgs e)

        {

 

 

            // TODO:  这行代码将数据加载到表csharpDataSet.people”中。您可以根据需要移动或删除它。

            this.peopleTableAdapter.Fill(this.csharpDataSet.people);

 

        }

 

        private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)

        {

            e.Row.HeaderCell.Value = (e.Row.Index + 1).ToString();

 

        }

        Color c;

        private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)

        {

 

            if (e.RowIndex >= 0)

            {

                c = dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor;

                dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightBlue;

            }

        }

 

        private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)

        {

 

            if (e.RowIndex >= 0)

            {

 

                dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = c;

            }

        }

    }

}

这期就到这里了,下次还有干货分享给大家,下期再见!


 

77
关闭
先学习,后交费申请表
每期5位名额
在线咨询
免费电话
QQ联系
先学习,后交费
TOP
您好,您想咨询哪门课程呢?
关于我们
机构简介
官方资讯
地理位置
联系我们
0513-91107100
周一至周六     8:30-21:00
微信扫我送教程
手机端访问
南通科迅教育信息咨询有限公司     苏ICP备15009282号     联系地址:江苏省南通市人民中路23-6号新亚大厦三楼             法律顾问:江苏瑞慈律师事务所     Copyright 2008-