我的程序框架大概是这样的:
代码省略了很多
private
void
Form1_Load(
object
sender, System.EventArgs e)
{
//这里初始化Form1里的dataGrid1,风格是系统默认的。
dataGrid1.DataSource = dt;
while(i<numCols)
{
DataGridTextBoxColumn tbc = new DataGridTextBoxColumn();
ts.GridColumnStyles.Add(tbc);
}
dataGrid1.TableStyles.Add(ts);
}
{
//这里初始化Form1里的dataGrid1,风格是系统默认的。
dataGrid1.DataSource = dt;
while(i<numCols)
{
DataGridTextBoxColumn tbc = new DataGridTextBoxColumn();
ts.GridColumnStyles.Add(tbc);
}
dataGrid1.TableStyles.Add(ts);
}
窗体里有个button1,它的事件是
private
void
button1_Click(
object
sender, System.EventArgs e)
{
//DataGridCellColorTextBoxColumn继承于DataGridTextBoxColumn,重写了Paint事件,另外它还加入了一个新的方法PaintCol,这个方法为了能重画单个单元格,代码在下面
DataGridCellColorTextBoxColumn columnTextColumn ;
for(int i = 0; i < numCols; ++i)
{
columnTextColumn = new DataGridCellColorTextBoxColumn();
columnTextColumn.CheckCellColor += new
CellColorEventHandlerSetColorValues);
tableStyle.GridColumnStyles.Add(columnTextColumn);
}
dataGrid1.TableStyles.Add(tableStyle);
dataGrid1.DataSource = ds.Tables["Quote"];
}
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);
}
{
//DataGridCellColorTextBoxColumn继承于DataGridTextBoxColumn,重写了Paint事件,另外它还加入了一个新的方法PaintCol,这个方法为了能重画单个单元格,代码在下面
DataGridCellColorTextBoxColumn columnTextColumn ;
for(int i = 0; i < numCols; ++i)
{
columnTextColumn = new DataGridCellColorTextBoxColumn();
columnTextColumn.CheckCellColor += new
CellColorEventHandlerSetColorValues);
tableStyle.GridColumnStyles.Add(columnTextColumn);
}
dataGrid1.TableStyles.Add(tableStyle);
dataGrid1.DataSource = ds.Tables["Quote"];
}
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);
}
窗体里有个button2,它的事件是
private
void
button2_Click(
object
sender, System.EventArgs e)
{
//通过调用DataGridCellColorTextBoxColumn里的PaintCol方法重画七行七列的单元格的外观
DataGridCellColorTextBoxColumn tbc =
(DataGridCellColorTextBoxColumn)dgt.GridColumnStyles[7];
Graphics g = dataGrid1.CreateGraphics();
Rectangle cellRect;
cellRect=dataGrid1.GetCellBounds(7,7);
CurrencyManager cm =
(CurrencyManager)dataGrid1.BindingContext[ds.Tablesdgt.MappingName]];
Brush fBrush = new System.Drawing.SolidBrush(Color.Red);
Brush bBrush= new System.Drawing.SolidBrush(Color.Black);
tbc.PaintCol(g, cellRect, cm, 7, bBrush, fBrush, false );
}
{
//通过调用DataGridCellColorTextBoxColumn里的PaintCol方法重画七行七列的单元格的外观
DataGridCellColorTextBoxColumn tbc =
(DataGridCellColorTextBoxColumn)dgt.GridColumnStyles[7];
Graphics g = dataGrid1.CreateGraphics();
Rectangle cellRect;
cellRect=dataGrid1.GetCellBounds(7,7);
CurrencyManager cm =
(CurrencyManager)dataGrid1.BindingContext[ds.Tablesdgt.MappingName]];
Brush fBrush = new System.Drawing.SolidBrush(Color.Red);
Brush bBrush= new System.Drawing.SolidBrush(Color.Black);
tbc.PaintCol(g, cellRect, cm, 7, bBrush, fBrush, false );
}
运行时先点button1,再点button2,则button2重画的单元格的风格会在窗体最小化和最
大化后变回button1执行后产生的风格。我现在的问题是就是怎样才能把button2产生的
风格一直保留下来?
谢谢
另外在实际应用过程中不是只通过button触发事件更改单元格的外观的
有可能根据某个事件来随时更改指定单元格的外观
还有谁能说说当窗口最小化后再最大化时 发生了什么事情?