DataGrid Cell Color Defaults On Click

F

Frederik

Hi all,

I extended the DataGridTextBoxColmun to be able to color an individual cell
in my datagrid. It works fine, but when I click on a cell, all the colored
cells in that row get the default color again. That's not what I intended.
The colored cells should stay colored, even when I click on them. What am I
doing wrong?

Thanks in advance,
Frederik

*************************
** start derived class **
*************************
public class DataGridTextBoxColumnEx :
DataGridTextBoxColumn
{
public void RowPaint(System.Data.DataTable dt,
Graphics g, Rectangle bounds, int rowNum)
{
CurrencyManager source =
(CurrencyManager)frmMain.ActiveForm.BindingContext[dt];
Brush backBrush = new SolidBrush(Color.DarkSalmon);
Brush foreBrush = new SolidBrush(Color.Black);
base.Paint(g, bounds, source, rowNum, backBrush,
foreBrush, false);
}
}
************************
** stop derived class **
************************


******************************************
** start method that calls Paint method **
******************************************
private void btnVerbeter_Click(object sender,
System.EventArgs e)
{
// grip krijgen op de achterliggende tabel
DataTable dt = ((DataView)DGridOpvraging.DataSource).Table;
// rechthoek van de cel
Rectangle cellRect;
// alle cellen doorlopen
for(int i = 0; i < dt.Rows.Count; i++)
{
if (!dt.Rows[2].ToString().Trim().Equals(
DGridOpvraging[i, 1].ToString().Trim()))
{
cellRect = DGridOpvraging.GetCellBounds(i, 1);
// Get the Graphics object for the form.
Graphics gr = DGridOpvraging.CreateGraphics();
// Paint methode aanroepen om de cel te kleuren
secondCol.RowPaint(dt, gr, cellRect, i);
}
}
}
*****************************************
** stop method that calls Paint method **
*****************************************
 
N

Nicholas Paldino [.NET/C# MVP]

Frederik,

I'm not quite sure I understand what you are trying to do. However,
based on what I have seen, I don't think that you are indicating the
re-paint correctly. If anything, in your button event handler, you should
call Invalidate on the data grid, which will force a repaint. Before you do
that, however, you should set whatever properties you need to in order for
the override of the Paint to correctly paint itself.

Hope this helps.
 

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