datagrid (winform) 改变单元格颜色方法调用问题

xiangyanpijiu 2005-01-21 12:09:42
我在程序中如何调用PaintCell()方法??
着段代码的主要功能是点datagrid的一个单元格是它边颜色

public class Form1: Form
{
protected DataGrid dataGrid1;
protected DataSet myDataSet;

private void PaintCell(object sender, MouseEventArgs e)
{
// Use the HitTest method to get a HitTestInfo object.
DataGrid.HitTestInfo hi;
DataGrid grid = (DataGrid)sender;
hi=grid.HitTest(e.X, e.Y);
// Test if the clicked area was a cell.
if(hi.Type == DataGrid.HitTestType.Cell)
{
// If it's a cell, get the GridTable and ListManager of the
// clicked table.
DataGridTableStyle dgt = dataGrid1.TableStyles[0];
CurrencyManager cm = (CurrencyManager)this.BindingContext[myDataSet.Tables[dgt.MappingName]];
// Get the Rectangle of the clicked cell.
Rectangle cellRect;
cellRect=grid.GetCellBounds(hi.Row, hi.Column);
// Get the clicked DataGridTextBoxColumn.
MyGridColumn gridCol =(MyGridColumn)dgt.GridColumnStyles[hi.Column];
// Get the Graphics object for the form.
Graphics g = dataGrid1.CreateGraphics();
// Create two new Brush objects, a fore brush, and back brush.
Brush fBrush = new System.Drawing.SolidBrush(Color.Blue);
Brush bBrush= new System.Drawing.SolidBrush(Color.Yellow);
// Invoke the Paint method to paint the cell with the brushes.
gridCol.PaintCol(g, cellRect, cm, hi.Row, bBrush, fBrush, false);
}
}
}

public class MyGridColumn:DataGridTextBoxColumn{
public void PaintCol(Graphics g, Rectangle cellRect,
CurrencyManager cm, int rowNum, Brush bBrush,
Brush fBrush, bool isVisible){
this.Paint(g, cellRect, cm, rowNum, bBrush, fBrush, isVisible);
}
}
...全文
148 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiangyanpijiu 2005-01-21
  • 打赏
  • 举报
回复
能不能把完整的例子提供出來,小弟愚笨。
xiaodele 2005-01-21
  • 打赏
  • 举报
回复
你 的思路不是很好,这样即使调用了一只是一次,如果界面有点变动 了就又回复的原始状态了,
给你一段代码,你看看有没有帮助

/// <summary>
/// 背景颜色设置事件
/// </summary>
public event FormatCellEventHandler SetCellFormat;

/// <summary>
/// Cell背景颜色的设置
/// </summary>
/// <param name="g"></param>
/// <param name="bounds"></param>
/// <param name="source"></param>
/// <param name="rowNum"></param>
/// <param name="backBrush"></param>
/// <param name="foreBrush"></param>
/// <param name="alignToRight"></param>
protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
{
DataGridFormatCellEventArgs e = new DataGridFormatCellEventArgs(rowNum, this.MappingName, this.DataGridTableStyle.DataGrid.Font, backBrush, foreBrush);
if(SetCellFormat != null)
{
SetCellFormat(this, e);
}
if(e.UseBaseClassDrawing)
base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
else
{
base.Paint(g, bounds, source, rowNum, e.BackBrush, e.ForeBrush,alignToRight);
}
if(e.TextFont != this.DataGridTableStyle.DataGrid.Font)
e.TextFont.Dispose();
}

在调用的时候只要加上 SetCellFormat 事件就可以了,
yichuan1982 2005-01-21
  • 打赏
  • 举报
回复
帮up
xiangyanpijiu 2005-01-21
  • 打赏
  • 举报
回复
頂一下。頂 一下。快啊。怎麽沒人回答???
xiangyanpijiu 2005-01-21
  • 打赏
  • 举报
回复
頂啊頂,頂啊頂。

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧