• 重写DataGridView


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using DEVGIS.Lib.Common;
    using DEVGIS.Common;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    
    
    namespace DEVGIS.Controls
    {
        public partial class MyDataGridView : DataGridView
        {
            private string filename;
    
    
            /// <summary>
            /// 保存的文件名
            /// </summary>
            public string FileName
            {
                get { return filename; }
                set { this.filename = value; }
            }
    
    
            public MyDataGridView()
            {
                InitializeComponent();
                InitItems();
            }
    
    
            public MyDataGridView(IContainer container)
            {
                container.Add(this);
    
    
                InitializeComponent();
                
                InitItems();       
            }
            private void InitItems()
            {
                this.BackgroundColor = Color.White;
    
    
                this.EnableHeadersVisualStyles = false;
    
    
                this.ColumnHeadersDefaultCellStyle.BackColor =Color.FromArgb(191,219,255);
    
    
                this.RowHeadersDefaultCellStyle.BackColor = Color.FromArgb(191, 219, 255);
    
    
                this.AllowUserToResizeRows = false;
    
    
                ContextMenuStrip cm = new ContextMenuStrip();
    
    
                cm.Items.Add("导出", null, mi_Click);
    
    
                this.ContextMenuStrip = cm;
            }
    
    
            void mi_Click(object sender, EventArgs e)
            {
                SaveFileDialog sa = new SaveFileDialog();
    
    
                sa.Filter = "Excel(2007)|*.xlsx";
    
    
                sa.FileName = FileName + "(" + DateTime.Now.ToString("yyyyMMddHHmmss") + ")";
    
    
                if (sa.ShowDialog() == DialogResult.OK)
                { 
                    DataExport de = new DataExport();
    
    
                    string path = sa.FileName;
    
    
                    bool b = de.DataGridviewShowToExcel(path, this);
    
    
                    if (this.Rows.Count > 0)
                    {
                        if (b)//为true,导出成功
                        {
                            PublicDim.ShowInfoMessage("信息导出成功!");
                        }
                        else
                        {
                            PublicDim.ShowInfoMessage("信息导出失败!");
                        }
                    }
                    else
                    {
                        PublicDim.ShowInfoMessage("没有可导出的数据!");
                    }
                }
            }
    
    
    
    
            protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
            {
                base.OnRowPostPaint(e);
                if (this.RowHeadersVisible)
                {
                    SetRowNumber(this, e);
                }
            }
    
    
            private void SetRowNumber(DataGridView dgvPackList, DataGridViewRowPostPaintEventArgs e)
            {
                System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(e.RowBounds.Location.X,
                                                       e.RowBounds.Location.Y,
                                                       dgvPackList.RowHeadersWidth - 4,
                                                       e.RowBounds.Height);
    
    
                TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                                    dgvPackList.RowHeadersDefaultCellStyle.Font,
                                    rectangle,
                                    dgvPackList.RowHeadersDefaultCellStyle.ForeColor,
                                    TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
            }
    
    
            protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
            {
                base.OnCellPainting(e);
                if (e.ColumnIndex == -1 && e.RowIndex == -1)
                {
                    bool mouseOver = e.CellBounds.Contains(PointToClient(Cursor.Position));
                    LinearGradientBrush brush = new LinearGradientBrush(
                        e.CellBounds,
                        mouseOver ? RowHeadersDefaultCellStyle.BackColor : RowHeadersDefaultCellStyle.BackColor,
                        RowHeadersDefaultCellStyle.BackColor,
                        LinearGradientMode.Vertical);
    
    
                    using (brush)
                    {
                        e.Graphics.FillRectangle(brush, e.CellBounds);
                        Rectangle border = e.CellBounds;
                        // border.Width -= 1;
                        e.Graphics.DrawRectangle(Pens.Gray, border);
                    }
    
    
                    e.PaintContent(e.CellBounds);
                    e.Handled = true;
                }
                else if (e.RowIndex == -1)
                {
                    //标题行    
                    bool mouseOver = e.CellBounds.Contains(PointToClient(Cursor.Position));
                    LinearGradientBrush brush = new LinearGradientBrush(
                        e.CellBounds,
                        mouseOver ? RowHeadersDefaultCellStyle.BackColor : RowHeadersDefaultCellStyle.BackColor,
                        RowHeadersDefaultCellStyle.BackColor,
                        LinearGradientMode.Vertical);
    
    
                    using (brush)
                    {
                        e.Graphics.FillRectangle(brush, e.CellBounds);
                        Rectangle border = e.CellBounds;
                        // border.Width -= 1;
                        e.Graphics.DrawRectangle(Pens.Gray, border);
                    }
    
    
                    e.PaintContent(e.CellBounds);
                    e.Handled = true;
                }
                else if (e.ColumnIndex == -1)
                {
                    //标题列
                    bool mouseOver = e.CellBounds.Contains(PointToClient(Cursor.Position));
                    LinearGradientBrush brush = new LinearGradientBrush(
                        e.CellBounds,
                        mouseOver ? RowHeadersDefaultCellStyle.BackColor : RowHeadersDefaultCellStyle.BackColor,
                        RowHeadersDefaultCellStyle.BackColor,
                        LinearGradientMode.Vertical);
    
    
                    using (brush)
                    {
                        e.Graphics.FillRectangle(brush, e.CellBounds);
                        Rectangle border = e.CellBounds;
                        // border.Width -= 1;
                        e.Graphics.DrawRectangle(Pens.Gray, border);
                    }
    
    
                    e.PaintContent(e.CellBounds);
                    e.Handled = true;
                }
            }
    
    
        }
    }
  • 相关阅读:
    django 我的博客 (慕课网视频)笔记
    读 django 中文文档投票例子笔记
    django的安装和初步使用
    Debug模式自定义NSlog
    重写NSString的setter方法
    iOS 常用代码之 UICollectionView
    生成100个 "20180520" 这样的时间字符串 写入txt文件
    WRNavigationBar 使用记录
    关于iphone设置显示模式为标准模式和放大模式时的区别
    CGContextRef 使用小记
  • 原文地址:https://www.cnblogs.com/devgis/p/16524169.html
Copyright © 2020-2023  润新知