By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,496 Members | 1,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,496 IT Pros & Developers. It's quick & easy.

DataGrid Refresh problem

P: n/a
I have a data grid refresh problem. I have a few columns and the first
column is data in the form of numbers. And in the form of the data
grid if I specify for example something like a code(in a text box) the
column of numbers should change colors depending on whether the number
was in the specified code(if it belongs to the code, color the cell
(first column) in Red and if not in some other color). It all works
out fine until I scroll up/down and trigger the paint event. The
changed colurs seem to be changed. I want the refreshing to be done
for all records for that column, but it seems it only refreshes parts
of the datagrid. How do I make the refreshing to be done to the whole
datagrid rather than a part of it, so I can get the original colur
change for each cell of the first column.

Code

//method to be passed into the coloumn style that determines the color
public Color MyGetColorRowCol(int row, int col)
{
// just conditionally set colors based on row, col values...

short number = 0;

short retval = 0;
Color c = Color.PaleGoldenrod;

if (col == 0)
{
number = array[cIndex];

retval = GetIfInChannelOrNot(code, number);

if(retval==0)
{
c = Color.LightGreen;
}

if(retval!=0)
{
c = Color.Red;
}

cIndex++;

if(TotalNumbers == cIndex)
{
cIndex = 0;
}

}

else
{
c = Color.LightGray;
}
return c;

}
//delegate required by custom column style

public delegate Color delegateGetColorRowCol(int row, int col);

public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
{
private delegateGetColorRowCol _getColorRowCol;
private int _column;

public DataGridColoredTextBoxColumn(delegateGetColorRowCo l
getcolorRowCol, int column)
{
_getColorRowCol = getcolorRowCol;
_column = column;
}
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)
{
// the idea is to conditionally set the foreBrush and/or backbrush
// depending upon some crireria on the cell value
// Here, we use a delegate to retrieve the color
try
{

backBrush = new SolidBrush(_getColorRowCol(rowNum, this._column));

foreBrush = new SolidBrush(Color.Black);
}

catch(Exception ex){ /* empty catch */ }
finally
{
// make sure the base class gets called to do the drawing with
// the possibly changed brushes
base.Paint(g, bounds, source, rowNum, backBrush, foreBrush,
alignToRight);
}
}
}
Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
James,

If you want the whole grid to be repainted, then you will want to call
the Invalidate method on the DataGrid, which will trigger a repaint of the
entire control.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"James" <ja*****************@yahoo.com.au> wrote in message
news:e6**************************@posting.google.c om...
I have a data grid refresh problem. I have a few columns and the first
column is data in the form of numbers. And in the form of the data
grid if I specify for example something like a code(in a text box) the
column of numbers should change colors depending on whether the number
was in the specified code(if it belongs to the code, color the cell
(first column) in Red and if not in some other color). It all works
out fine until I scroll up/down and trigger the paint event. The
changed colurs seem to be changed. I want the refreshing to be done
for all records for that column, but it seems it only refreshes parts
of the datagrid. How do I make the refreshing to be done to the whole
datagrid rather than a part of it, so I can get the original colur
change for each cell of the first column.

Code

//method to be passed into the coloumn style that determines the color
public Color MyGetColorRowCol(int row, int col)
{
// just conditionally set colors based on row, col values...

short number = 0;

short retval = 0;
Color c = Color.PaleGoldenrod;

if (col == 0)
{
number = array[cIndex];

retval = GetIfInChannelOrNot(code, number);

if(retval==0)
{
c = Color.LightGreen;
}

if(retval!=0)
{
c = Color.Red;
}

cIndex++;

if(TotalNumbers == cIndex)
{
cIndex = 0;
}

}

else
{
c = Color.LightGray;
}
return c;

}
//delegate required by custom column style

public delegate Color delegateGetColorRowCol(int row, int col);

public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
{
private delegateGetColorRowCol _getColorRowCol;
private int _column;

public DataGridColoredTextBoxColumn(delegateGetColorRowCo l
getcolorRowCol, int column)
{
_getColorRowCol = getcolorRowCol;
_column = column;
}
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)
{
// the idea is to conditionally set the foreBrush and/or backbrush
// depending upon some crireria on the cell value
// Here, we use a delegate to retrieve the color
try
{

backBrush = new SolidBrush(_getColorRowCol(rowNum, this._column));

foreBrush = new SolidBrush(Color.Black);
}

catch(Exception ex){ /* empty catch */ }
finally
{
// make sure the base class gets called to do the drawing with
// the possibly changed brushes
base.Paint(g, bounds, source, rowNum, backBrush, foreBrush,
alignToRight);
}
}
}

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.