大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新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账号...