为了需要,写了一个用于DataGrid的组件。(因为DataGrid中的bool变灰的时候带钩,我需要不带钩的)。碰到的问题是,运行程序的时候没有调用这个组件的Paint()重载方法。请问如何实现主界面每次重绘的时候都调用组件的Paint()方法?
===========================================================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.IO;namespace WindowsApplication1
{
/// <summary>
/// DataGridCheckBoxColumn 的摘要说明。
/// </summary>
public class DataGridCheckBoxColumn : DataGridTextBoxColumn
{
public event DataGridCellButtonClickEventHandler CellButtonClicked; private Bitmap _uncheck;
private Bitmap _checked;
private Bitmap _disenable;
private int _columnNum;
private int _pressedRow;
private CheckBox checkBox; public CheckBox CheckBox
{
get { return checkBox; }
} public DataGridCheckBoxColumn(int colNum)
{
_columnNum = colNum;
_pressedRow = -1; this.checkBox = new CheckBox();
this.checkBox.Enabled = false;
this.checkBox.Checked = false;

try
{
char[] c={','};
System.IO.Stream strm = this.GetType().Assembly.GetManifestResourceStream(this.GetType().Assembly.ToString().Split(c,10)[0]+".Checked.bmp");
_checked = new Bitmap(strm); strm = this.GetType().Assembly.GetManifestResourceStream(this.GetType().Assembly.ToString().Split(c,10)[0]+".Unchecked.bmp");
_uncheck = new Bitmap(strm); strm = this.GetType().Assembly.GetManifestResourceStream(this.GetType().Assembly.ToString().Split(c,10)[0]+".Disenable.bmp");
_disenable = new Bitmap(strm);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
} private void DrawButton(Graphics g, Bitmap bm, Rectangle bounds, int row)
{ DataGrid dg = this.DataGridTableStyle.DataGrid;
string s = dg[row, this._columnNum].ToString(); SizeF sz = g.MeasureString(s, dg.Font, bounds.Width - 4, StringFormat.GenericTypographic); int x = bounds.Left + Math.Max(0, (bounds.Width - (int)sz.Width)/2);
g.DrawImage(bm, bounds, 0, 0, bm.Width, bm.Height,GraphicsUnit.Pixel);

// if(sz.Height < bounds.Height)
// {
// int y = bounds.Top + (bounds.Height - (int) sz.Height) / 2;
// if(this._checked == bm)
// {
// x++;
// }
//
// g.DrawString(s, dg.Font, new SolidBrush(dg.ForeColor), x, y);
// } } public void HandleMouseDown(object sender, MouseEventArgs e)
{
DataGrid dg = this.DataGridTableStyle.DataGrid;
DataGrid.HitTestInfo hti = dg.HitTest(new Point(e.X, e.Y));
bool isClickInCell = (hti.Column == this._columnNum
&& hti.Row > -1); Bitmap bm = null;
if (this.checkBox.Enabled) bm = (checkBox.Checked) ? this._checked : this._uncheck; if (this.checkBox.Enabled == false) bm = this._disenable; Rectangle rect = new Rectangle(0,0,0,0);
if(isClickInCell)
{
rect = dg.GetCellBounds(hti.Row, hti.Column);
isClickInCell = (e.X > rect.Right - bm.Width);
} if(isClickInCell)
{
//Console.WriteLine("HandleMouseDown " + hti.Row.ToString());
Graphics g = Graphics.FromHwnd(dg.Handle);
//g.DrawImage(this._buttonPressed, rect.Right - this._buttonPressed.Width, rect.Y);
DrawButton(g, bm, rect, hti.Row);
g.Dispose();
_pressedRow = hti.Row;
}
} public void HandleMouseUp(object sender, MouseEventArgs e)
{
DataGrid dg = this.DataGridTableStyle.DataGrid;
DataGrid.HitTestInfo hti = dg.HitTest(new Point(e.X, e.Y));
bool isClickInCell = (hti.Column == this._columnNum
&& hti.Row > -1); _pressedRow = -1;
Rectangle rect = new Rectangle(0,0,0,0); Bitmap bm = null;
if (this.checkBox.Enabled) bm = (checkBox.Checked) ? this._checked : this._uncheck; if (this.checkBox.Enabled == false) bm = this._disenable; if(isClickInCell)
{
rect = dg.GetCellBounds(hti.Row, hti.Column);
isClickInCell = (e.X > rect.Right - bm.Width);
}
if(isClickInCell)
{
Graphics g = Graphics.FromHwnd(dg.Handle);
// g.DrawImage(this._button, rect.Right - this._button.Width, rect.Y);
DrawButton(g, bm, rect, hti.Row);
g.Dispose();
if(CellButtonClicked != null)
CellButtonClicked(this, new DataGridCellButtonClickEventArgs(hti.Row, hti.Column));
}
} protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
{
// base.Paint (g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
DataGrid parent = this.DataGridTableStyle.DataGrid;
bool current = parent.IsSelected(rowNum) ||
( parent.CurrentRowIndex == rowNum && 
parent.CurrentCell.ColumnNumber == this._columnNum);

Bitmap bm = this.checkBox.Checked ? this._checked : this._uncheck;

if (this.checkBox.Enabled == false) bm = this._disenable;

this.DrawButton(g, bm, bounds, rowNum);
}
} public delegate void DataGridCellButtonClickEventHandler(object sender, DataGridCellButtonClickEventArgs e); public class DataGridCellButtonClickEventArgs : EventArgs
{
private int _row;
private int _col; public DataGridCellButtonClickEventArgs(int row, int col)
{
_row = row;
_col = col;
} public int RowIndex {get{return _row;}}
public int ColIndex {get{return _col;}}
}
}

解决方案 »

  1.   

    主窗体代码:rivate void Form1_Load(object sender, System.EventArgs e)
    {
    DataTable dt = new DataTable(); DataGridTableStyle tableStyle = new DataGridTableStyle();
    tableStyle.MappingName = "Book"; // 是否升级
    dt.Columns.Add(new DataColumn("UpdateOption", typeof(bool)));
    dt.Columns["UpdateOption"].DefaultValue = false; DataGridCheckBoxColumn checkboxColStyle = null;
    int i = 0; checkboxColStyle = new DataGridCheckBoxColumn(i); //pass the column#
    checkboxColStyle.HeaderText =  "升级";
    checkboxColStyle.MappingName =  dt.Columns["UpdateOption"].ColumnName;
    checkboxColStyle.CheckBox.Enabled = false; //hookup our cellbutton handler...
    checkboxColStyle.CellButtonClicked += 
    new DataGridCellButtonClickEventHandler(HandleCellButtonClick); tableStyle.GridColumnStyles.Add(checkboxColStyle); //hook the mouse handlers
    dataGrid1.MouseDown += new MouseEventHandler(checkboxColStyle.HandleMouseDown);
    dataGrid1.MouseUp += new MouseEventHandler(checkboxColStyle.HandleMouseUp); // make the dataGrid use our new tablestyle and bind it to our table
    dataGrid1.TableStyles.Clear();
    dataGrid1.TableStyles.Add(tableStyle); DataRow row = dt.NewRow();
    row[0] = false;
    dt.Rows.Add(row); //bind the table to the datagrid
    dataGrid1.DataSource = dt;
    }// handler for a click on one of the gridcell buttons
    private void HandleCellButtonClick(object sender, DataGridCellButtonClickEventArgs e)
    {
    MessageBox.Show("row " + e.RowIndex.ToString() +  " will be deleted.");
    }
      

  2.   

    如果你仅仅是去掉boolcolumnstyle中你所说的带勾的话,你只要设置AllowNull=false就可以了