469,887 Members | 1,912 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,887 developers. It's quick & easy.

DataGrid Refresh problem

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
1 6060
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.

Similar topics

5 posts views Thread by Jeff | last post: by
3 posts views Thread by Matthew Woods | last post: by
3 posts views Thread by Jim Heavey | last post: by
8 posts views Thread by Inigo Jimenez | last post: by
3 posts views Thread by james crosthwaite via .NET 247 | last post: by
1 post views Thread by Andre | last post: by
7 posts views Thread by Juan Romero | last post: by
1 post views Thread by Kevin Hodgson | last post: by
2 posts views Thread by Dan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.