gridview属性_datagridview设置列宽

gridview属性_datagridview设置列宽usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Drawing;usingSystem.Windows.Forms;classSetDataViewGirdStyle{   privatestaticColor_mLinearBeginColor;

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

class SetDataViewGirdStyle
{

    private static Color _mLinearBeginColor;
    private static Color _mLinearEndColor;
    private static Color _mBorderColor;
    private static bool _IsDrawing2D;

    public SetDataViewGirdStyle()
    { }

    /// <summary>
    /// 设置DataGirdView显示颜色
    /// </summary>
    /// <param name=”dgv”>目标DataGirdView对象</param>
    /// <param name=”BackgroundColor”>DataGirdView背景色</param>
    /// <param name=”SelectionBackColor”>选定行的背景色</param>
    /// <param name=”SelectionForeColor”>选定行的前景色</param>
    /// <param name=”CellSelectionBackColor”>单元格被选定时的背景色</param>
    /// <param name=”RowBackColor”>默认行背景色</param>
    /// <param name=”ImparRowBackColor”>默认奇数行背景色</param>
    /// <param name=”IsDrawRowHeaders”>是否绘制行标题颜色</param>
    /// <param name=”IsDrawing2D”>是否启用二维绘制行标题颜色(渐变色)</param>
    /// <param name=”mLinearBeginColor”>渐变色起始颜色</param>
    /// <param name=”mLinearEndColor”>渐变色结束颜色</param>
    /// <param name=”mBorderColor”>边框颜色</param>
    public static void SetDgvViewColor(DataGridView dgv, Color BackgroundColor,
                                Color SelectionBackColor, Color SelectionForeColor,
                                Color CellSelectionBackColor, Color RowBackColor,
                                Color ImparRowBackColor, bool IsDrawRowHeaders,
                                bool IsDrawing2D, Color mLinearBeginColor,
                                Color mLinearEndColor, Color mBorderColor)
    {

        dgv.BackgroundColor = BackgroundColor;
        dgv.DefaultCellStyle.SelectionBackColor = SelectionBackColor;
        dgv.DefaultCellStyle.SelectionForeColor = SelectionForeColor;
        dgv.RowHeadersDefaultCellStyle.SelectionBackColor = CellSelectionBackColor;
        dgv.RowsDefaultCellStyle.BackColor = RowBackColor;
        dgv.AlternatingRowsDefaultCellStyle.BackColor = ImparRowBackColor;
        if (IsDrawRowHeaders)
        {

            _mLinearBeginColor = mLinearBeginColor;
            _mLinearEndColor = mLinearEndColor;
            _mBorderColor = mBorderColor;
            _IsDrawing2D = IsDrawing2D;
            dgv.CellPainting += new DataGridViewCellPaintingEventHandler(dgv_CellPainting);
        }
    }

    private static void CellPainting(DataGridViewCellPaintingEventArgs e)
    {

        Color mLinearColor1 = _mLinearBeginColor;
        Color mLinearColor2 = _mLinearEndColor;
        Color mGridColor = _mBorderColor;

        System.Drawing.StringFormat StrFormat = new StringFormat();
        Rectangle Rect = new Rectangle(e.CellBounds.X – 1, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height – 1);
        System.Drawing.Drawing2D.LinearGradientBrush LinearGradientBrush = null;
        if (_IsDrawing2D)
        {

            LinearGradientBrush = new System.Drawing.Drawing2D.LinearGradientBrush(Rect, mLinearColor1, mLinearColor2, System.Drawing.Drawing2D.LinearGradientMode.Vertical);
        }
        else
        {

            LinearGradientBrush = new System.Drawing.Drawing2D.LinearGradientBrush(Rect, mLinearColor1, mLinearColor1, System.Drawing.Drawing2D.LinearGradientMode.Vertical);
        }

        try
        {

            if (e.RowIndex == -1 || e.ColumnIndex == -1)
            {

                e.Graphics.FillRectangle(LinearGradientBrush, Rect);
                e.Graphics.DrawRectangle(new Pen(mGridColor), Rect);
                e.PaintContent(e.CellBounds);
                e.Handled = true;
            }
        }
        catch (Exception ex)
        {

            throw new Exception(ex.Message);
        }
        finally
        {

            if (StrFormat != null)
                StrFormat.Dispose();
            if (LinearGradientBrush != null)
                LinearGradientBrush.Dispose();
        }
    }

    private static void dgv_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {

        CellPainting(e);
    }

    /// <summary>
    /// 设置DataGirdView样式
    /// </summary>
    /// <param name=”dgv”>目标DataGirdView对象</param>
    /// <param name=”DockStyleType”>指定控键停靠的位置和方式</param>
    /// <param name=”BorderStyleType”>指定控键边框样式</param>
    /// <param name=”IsOrderCol”>指定用户是否可手动调整列位置</param>
    /// <param name=”IsReadOnly”>指定用户是否可编辑单元格</param>
    /// <param name=”IsMultiSelect”>指定用户是否可以选择多行</param>
    /// <param name=”IsResizeCol”>指定用户是否可以自己调整列大小(数据量大时请慎用)</param>
    /// <param name=”IsResizeRow”>指定用户是否可以自己调整行大小(数据量大时请慎用)</param>
    /// <param name=”IsRowHeaders”>是否显示行标题</param>
    /// <param name=”RowHeadersWidth”>行标题显示的宽度(当IsRowHeaders=True时HeadersWidth必须大于或等于4;当HeadersWidth大于或等于24时出现行标箭头)</param>
    /// <param name=”RowCopy”>行值复制模式</param>
    /// <param name=”IsShowIndex”>是否在行标题显示序号</param>
    public static void SetDgvViewStyle(DataGridView dgv, DockStyle DockStyleType,
                                BorderStyle BorderStyleType, bool IsOrderCol,
                                bool IsReadOnly, bool IsMultiSelect,
                                bool IsResizeCol, bool IsResizeRow,
                                bool IsRowHeaders, int RowHeadersWidth,
                                DataGridViewClipboardCopyMode RowCopy, bool IsShowIndex)
    {

        dgv.Dock = DockStyleType;
        dgv.BorderStyle = BorderStyleType;
        dgv.AllowUserToAddRows = false;
        dgv.AllowUserToDeleteRows = false;
        dgv.AllowUserToOrderColumns = IsOrderCol;
        dgv.ReadOnly = !IsReadOnly;
        dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        dgv.MultiSelect = IsMultiSelect;
        dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
        dgv.AllowUserToResizeColumns = IsResizeCol;
        if (IsResizeCol)
        {

            dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
        }
        else
        {

            dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        }
        dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
        dgv.AllowUserToResizeRows = IsResizeRow;
        if (IsResizeRow)
        {

            dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
        }
        else
        {

            dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
        }
        dgv.RowHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
        dgv.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing;
        dgv.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
        dgv.RowHeadersVisible = IsRowHeaders;
        if (dgv.RowHeadersVisible && RowHeadersWidth < 4)
        {

            throw new Exception(“RowHeadersWidth Error”);
        }
        dgv.RowHeadersWidth = RowHeadersWidth;
        dgv.ClipboardCopyMode = RowCopy;
        if (IsShowIndex)
            dgv.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dgv_RowPostPaint);
    }

    private static void dgv_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
    {

        Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, ((DataGridView)sender).RowHeadersWidth, e.RowBounds.Height);
        TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), ((DataGridView)sender).RowHeadersDefaultCellStyle.Font, rectangle, ((DataGridView)sender).RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/190012.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • Hi3516DV300双目开发板开发环境搭建

    Hi3516DV300双目开发板开发环境搭建Hi3516DV300双目开发板开发环境搭建1、软件包安装参考:《Hi3516CV500╱Hi3516DV300SDK安装及升级使用说明》将sdk和交叉工具链文件在同一目录展开sdk包1.进入Hi3516CV500_SDK_V2.0.1.0目录下执行./sdk.unpack(请用root或sudo权限执行)2.解压tar–xzfarm-himix200-linux.tgz,进入arm-himix200-linux目录运行chmod+xarm-himix200-l

  • Redisson 实现分布式锁

    Redisson 实现分布式锁

  • vb.net 学习「建议收藏」

    vb.net 学习「建议收藏」一、vb.net是什么vb:即VisualBasic.net:是一个开发平台,在这个平台上可以开发多种语言。bcl:我的理解就是类的定义clr:我的理解就是类的使用。作用:(相当于java虚拟机,内存管理,垃圾清理)注:因为百度上说的都太宏观了,目前还是刚刚接触这个,是从名字上去理解,有其他见解可以留言或后面我有不同感悟再改二、vb.net有什么用目前我接触到:控制台程序 窗体程序 webservice三、怎么用说一个可能很多人忽略或只有我忽略了一下吧.

  • 【C语言】——背包问题详解「建议收藏」

    【C语言】——背包问题详解「建议收藏」1.题目描述:——背包问题有若干物品,每种物品的价值和重量各不相同,将物品装入一个容量有限的背包,如何选择装入的物品,使背包的价值最大。2.题目分析:要是背包中的物品价值最大,则需要在有限的重量中尽可能装入价值更大的物品,基于这种思想则采取贪心算法首先计算物品的单位价值,即价值/重量,根据单位价值对物品进行排序,优先装入单位价值高的物品,直至背包装满。3.代码实现:#include<stdio.h>intn;//物品数量doublec;//背包容量…

  • 连接ftp服务器 JDK 1.7

    连接ftp服务器 JDK 1.7

  • P3381 【模板】最小费用最大流

    P3381 【模板】最小费用最大流

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号