141 lines
5.1 KiB
C#
141 lines
5.1 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Reflection;
|
|
using System.Windows.Forms;
|
|
|
|
namespace JinYuan.Helper
|
|
{
|
|
public static class DataGridViewHelper
|
|
{
|
|
/// <summary>
|
|
/// 给DataGridView添加行号
|
|
/// </summary>
|
|
/// <param name="dgv">dgv控件</param>
|
|
/// <param name="e">dgv参数</param>
|
|
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, "操作失败");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 给DataGridView指定列改变前景色
|
|
/// </summary>
|
|
/// <param name="dgv"></param>
|
|
/// <param name="e"></param>
|
|
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, "操作失败");
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 给DataGridView绘制边框
|
|
/// </summary>
|
|
/// <param name="dgv">dgv控件</param>
|
|
/// <param name="e">dgv参数</param>
|
|
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));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 奇偶换色
|
|
/// </summary>
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 双缓冲,解决闪烁问题
|
|
/// </summary>
|
|
/// <param name="dgv"></param>
|
|
/// <param name="flag"></param>
|
|
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);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 双缓冲,解决闪烁问题
|
|
/// </summary>
|
|
/// <param name="lv"></param>
|
|
/// <param name="flag"></param>
|
|
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);
|
|
}
|
|
}
|
|
}
|