using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace JinYuan.Helper
{
public static class DataGridViewHelper
{
///
/// 给DataGridView添加行号
///
/// dgv控件
/// dgv参数
public static void DgvRowPostPaint(DataGridView dgv, DataGridViewRowPostPaintEventArgs e)
{
try
{
//添加行号
SolidBrush solidBrush = new SolidBrush(dgv.RowHeadersDefaultCellStyle.ForeColor);
string lineNo = (e.RowIndex + 1).ToString();
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
e.Graphics.DrawString(lineNo, e.InheritedRowStyle.Font, solidBrush, new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgv.RowHeadersWidth, dgv.RowTemplate.Height), sf);
}
catch (Exception ex)
{
MessageBox.Show("添加行号时发生错误,错误信息:" + ex.Message, "操作失败");
}
}
///
/// 给DataGridView指定列改变前景色
///
///
///
public static void DgvRowPrePaint(DataGridView dgv,string Column, object sender, DataGridViewRowPrePaintEventArgs e)
{
if (e.RowIndex >= dgv.Rows.Count - 1)
return;
DataGridViewRow dr = (sender as DataGridView).Rows[e.RowIndex];
try
{
if(dr.Cells[Column].Value != null)
{
if (dr.Cells[Column].Value.Equals("OK"))
{
// 设置单元格的背景色
//dr.Cells[Column].Style.BackColor = Color.LightGreen;
// 设置单元格的前景色
dr.Cells[Column].Style.ForeColor = Color.LightGreen;
}
else
{
//dr.Cells[Column].Style.BackColor = Color.Red;
dr.Cells[Column].Style.ForeColor = Color.Red;
}
}
}
catch (Exception ex)
{
MessageBox.Show("更改状态时发生错误,错误信息:" + ex.Message, "操作失败");
}
}
///
/// 给DataGridView绘制边框
///
/// dgv控件
/// dgv参数
public static void DgvRowPaint(DataGridView dgv, PaintEventArgs e, Color borderColor)
{
e.Graphics.DrawRectangle(new Pen(borderColor), new Rectangle(0, 0, dgv.Width - 1, dgv.Height - 1));
}
///
/// 奇偶换色
///
public static void DgvStyle(DataGridView dgv, Color defaultBackColor, Color alternatingBackColor, Color gridColor)
{
//奇数行的背景色
dgv.AlternatingRowsDefaultCellStyle.BackColor = alternatingBackColor;
dgv.AlternatingRowsDefaultCellStyle.SelectionBackColor = alternatingBackColor;
//默认的行样式
dgv.RowsDefaultCellStyle.BackColor = defaultBackColor;
dgv.RowsDefaultCellStyle.SelectionBackColor = defaultBackColor;
dgv.RowHeadersDefaultCellStyle.BackColor = defaultBackColor;
dgv.RowHeadersDefaultCellStyle.SelectionBackColor = defaultBackColor;
//数据网格颜色
dgv.GridColor = gridColor;
}
///
/// 双缓冲,解决闪烁问题
///
///
///
public static void DoubleBufferedDataGirdView(this DataGridView dgv, bool flag)
{
Type dgvType = dgv.GetType();
PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(dgv, flag, null);
}
///
/// 双缓冲,解决闪烁问题
///
///
///
public static void DoubleBufferedListView(this ListView lv, bool flag)
{
Type lvType = lv.GetType();
PropertyInfo pi = lvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(lv, flag, null);
}
public static void DoubleBufferedListBox(this ListBox lb, bool flag)
{
Type lvType = lb.GetType();
PropertyInfo pi = lvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(lb, flag, null);
}
}
}