DataGrid Refresh problem

J

James

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(delegateGetColorRowCol
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);
}
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

James said:
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(delegateGetColorRowCol
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);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top