Every 5th Datagrid Line a different colour

  • Thread starter Thread starter midgetgem
  • Start date Start date
M

midgetgem

Hello,

Can someone tell me how to change the 5th datagrid line colour? I can
change all of them, but not only the fifth ones.

Any help much appreciated!

Cheers
G
 
Hi,

midgetgem said:
Hello,

Can someone tell me how to change the 5th datagrid line colour? I can
change all of them, but not only the fifth ones.

Any help much appreciated!

Cheers
G

Well, I've did this by subclassing the DataGridColumnStyle's that represent
a column, override the Paint method and set the background brush to a
different color.

The paint function to override is:
http://msdn.microsoft.com/library/d...sformsdatagridcolumnstyleclasspainttopic3.asp

So what you do is subclass the datagrid column styles you use (eg
DataGridTextBoxColumn and/or DataGridBoolColumn), override the Paint method
like this:

protected internal override void Paint (
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
Brush backBrush,
Brush foreBrush,
bool alignToRight )
{
if( ( rowNum + 1 ) % 5 == 0 )
backBrush = Brushes.Aqua; // any brush you want

base.Paint (
g,
bounds,
source,
rowNum,
backBrush,
foreBrush,
alignToRight
);
}

I don't know how this behaves when you apply sorting so you might want to
test this first. I never really used rowNum to determine anything but the
data in the currency manager.

You then add instances of your grid column style class(es) to the DataGrid's
table style associated with the DataTable. A drawback is that the Paint
method will be called for each cell.

Kind regards,
 
Thanks for your help Tom.

I've added the overriden Paint method in a class called
DataGridColoredTextBoxColumn at the end of my code-behind file.
However, my datagrid never seems to use this paint method!

I have a 2D array as my datasource, not a datatable, but I've added a
TableStyle with my DataGridColoredTextBoxColumn as the
GridColumnStyles, but this doesn't seem to be enough.

Do you know what I should do to get my grid to use the overriden paint
method?

Thanks

Gemma
 
Hi Gemma,

midgetgem said:
Thanks for your help Tom.

I've added the overriden Paint method in a class called
DataGridColoredTextBoxColumn at the end of my code-behind file.
However, my datagrid never seems to use this paint method!

I have a 2D array as my datasource, not a datatable, but I've added a
TableStyle with my DataGridColoredTextBoxColumn as the
GridColumnStyles, but this doesn't seem to be enough.

Do you know what I should do to get my grid to use the overriden paint
method?

Thanks
Gemma

I think it is not possible to do that with a 2D array, I'm not entirely sure
because I've never used a DataGrid with a regular array or ArrayList. The
MappingName property of a DataGridColumnStyle (the base class for any
datagrid column style class) should be set to a column's name in a data
source. You can do that for a DataTable which represents a table with
columns that have names, but not for a 2D array which doesn't have that kind
of information.

I suggest you use a DataTable instead of a 2D array as the data source for
the DataGrid. Set the MappingName property of the column styles you add to
the grid's table style, to the corresponding columns in the DataTable.

DataTable info from MSDN contains a small example of setting up a DataTable
structure, and adding data to it:
http://msdn.microsoft.com/library/d...f/html/frlrfsystemdatadatatableclasstopic.asp

Hope this helps,
 
Ok, I've changed my source to a datatable. I have the overriden paint
method in a class called DataGridColoredTextBoxColumn, and add it to my
grid by creating an instance of it, looping through my columns and
setting the Mapping Name and Header Text to the column name, adding the
textboxcolumn ot the tablestyle and adding the table style to the grid.

However, this still doesn't seem to use my paint method. Is there some
way I can force it do that?

Thanks
Gemma
 
Does anyone know how I can do this?

It seems to be calling the base paint method but not my overriden one.
Does anyone have any clue as to why it doesn't seem to use the table
style that I add to the grid?

Any help or suggestions would be much appreciated!

Cheers
Gemma
 
Hi Gemma,

midgetgem said:
Ok, I've changed my source to a datatable. I have the overriden paint
method in a class called DataGridColoredTextBoxColumn, and add it to my
grid by creating an instance of it, looping through my columns and
setting the Mapping Name and Header Text to the column name, adding the
textboxcolumn ot the tablestyle and adding the table style to the grid.

However, this still doesn't seem to use my paint method. Is there some
way I can force it do that?

Thanks
Gemma

Sorry for the late response. Could you post the code for the
DataGridColoredTextBoxColumn class, and the form class that has the grid on
it?

The things I described should work.

Kind regards,
 
OK, this is my DataGridColoredTextBoxColumn class:
public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
{
public DataGridColoredTextBoxColumn() : base()
{}

// based on
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q620q
protected override void Paint(Graphics g, Rectangle bounds,
CurrencyManager source,
int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
{
// the idea is to conditionally set the foreBrush and/or backbrush
// depending upon some criteria on the cell value
// Here, we color 0 as white, 1 as black, 2 as cross
string cellValue = (this.GetColumnValueAtRow(source,
rowNum)).ToString();
switch(cellValue)
{
case "0": foreBrush = new SolidBrush(Color.Black);
g.FillRectangle(backBrush, bounds.X, bounds.Y, bounds.Width,
bounds.Height);
Font font = new Font(FontFamily.GenericSansSerif, (float)8 );
g.DrawString( cellValue, font, Brushes.White, bounds.X,
bounds.Y);
break;
case "1": foreBrush = new SolidBrush(Color.Black);
break;
case "X": foreBrush = new SolidBrush(Color.White);
break;
}

if((rowNum + 1) % 5 == 0)
{
backBrush = Brushes.Red; // any brush you want
}

// the base class gets called to do the drawing with
// the possibly changed brushes
base.Paint(g, bounds, source, rowNum, backBrush, foreBrush,
alignToRight);
}
}

and my form behind code declares it using
private System.Windows.Forms.DataGrid puzzleDataGrid;
private DataGridColoredTextBoxColumn colouredTBCol;
and use it here:
pixelPixGrid = new Grid(path);

DataGridTableStyle tableStyle = new DataGridTableStyle();
DataGridColoredTextBoxColumn colTbCol;

for (int i=0; i <= pixelPixGrid.noOfColsInGrid(); i++) // including
extra column
{
colTbCol = new DataGridColoredTextBoxColumn();
colTbCol.HeaderText =
pixelPixGrid.getGridDataTable().Columns.ColumnName;
colTbCol.MappingName =
pixelPixGrid.getGridDataTable().Columns.ColumnName;

// Adds our column style to the table style collection
tableStyle.GridColumnStyles.Add(colTbCol);
}

// Now, make the dataGrid use our own tablestyle and bind it to our
table
puzzleDataGrid.TableStyles.Clear();
puzzleDataGrid.TableStyles.Add(tableStyle);

// set puzzle
puzzleDataGrid.DataSource = pixelPixGrid.getGridDataTable();

and that's about it really, it never breaks in my overridden paint
method.

Thanks for your help, and I look forward to your reply!

Gemma
 
Hi Gemma,

"midgetgem" <[email protected]> schreef in bericht
[...]
and that's about it really, it never breaks in my overridden paint
method.

Thanks for your help, and I look forward to your reply!
Gemma

I'll give you a small example, one that works as I explained. Maybe you can
use it to correct whatever you're doing wrong.

// -- form code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GridTest
{
public partial class Form1 : Form
{
private DataTable gridData;

public Form1()
{
InitializeComponent( );

InitializeDS( );
InitializeGrid( );
FillGrid( );
}

private void FillGrid( )
{
for( int i = 0; i != 1000; ++i )
{
DataRow dr = gridData.NewRow( );
dr[ "Value" ] = i;
gridData.Rows.Add( dr );
}
}

private void InitializeDS( )
{
gridData = new DataTable( );
gridData.Columns.Add( new DataColumn( "Value", typeof( string ) ) );
ValueGrid.SetDataBinding( gridData, null );
}

private void InitializeGrid( )
{
DataGridColoredTextCol dgctc = new DataGridColoredTextCol( );
dgctc.Format = "";
dgctc.FormatInfo = null;
dgctc.HeaderText = "Value";
dgctc.MappingName = "Value";
dgctc.Width = 200;
dgctc.ReadOnly = true;

DataGridTableStyle dgts = new DataGridTableStyle( );
dgts.GridColumnStyles.AddRange ( new
System.Windows.Forms.DataGridColumnStyle[ ] { dgctc } );

ValueGrid.TableStyles.AddRange( new
System.Windows.Forms.DataGridTableStyle[ ] { dgts } );
}
}
}

// -- designer generated code
namespace GridTest
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ValueGrid = new System.Windows.Forms.DataGrid( );
( (System.ComponentModel.ISupportInitialize) (
this.ValueGrid ) ).BeginInit( );
this.SuspendLayout( );
//
// ValueGrid
//
this.ValueGrid.DataMember = "";
this.ValueGrid.HeaderForeColor =
System.Drawing.SystemColors.ControlText;
this.ValueGrid.Location = new System.Drawing.Point( 12, 12 );
this.ValueGrid.Name = "ValueGrid";
this.ValueGrid.ReadOnly = true;
this.ValueGrid.Size = new System.Drawing.Size( 592, 443 );
this.ValueGrid.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF( 6F, 13F );
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size( 616, 467 );
this.Controls.Add( this.ValueGrid );
this.Name = "Form1";
this.Text = "Form1";
( (System.ComponentModel.ISupportInitialize) (
this.ValueGrid ) ).EndInit( );
this.ResumeLayout( false );
}
#endregion

private System.Windows.Forms.DataGrid ValueGrid;
}
}

// -- The DataGridColoredTextCol class
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace GridTest
{
public class DataGridColoredTextCol : DataGridTextBoxColumn
{
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 )
{
if( ( rowNum + 1 ) % 5 == 0 )
backBrush = System.Drawing.Brushes.Red;
base.Paint( g, bounds, source, rowNum, backBrush, foreBrush,
alignToRight );
}
}
}

Kind regards,
 
Gemma,

Was my code example enough to solve your situation? If not, I suggest you
email me directly and attach your project then I'll have a look into it.

Kind regards,
 
Thanks for your help Tom, I'm going to go through it now, and I'll let
you know how it goes!

Gemma
 
Ok, it still doesn't work, so I've tried emailing you. I've managed to
test my paint method on your GridTest, but this doesn't actually change
the colour of the grid line - it changes the colour of the row. Do you
know if I could change the grid line so it appears like a divider for
every block of five rows?

Thanks for your help,

Gemma
 
Hi Gemma,

midgetgem said:
Ok, it still doesn't work, so I've tried emailing you. I've managed to
test my paint method on your GridTest, but this doesn't actually change
the colour of the grid line - it changes the colour of the row. Do you
know if I could change the grid line so it appears like a divider for
every block of five rows?

Thanks for your help,
Gemma

Mmm, I might have misunderstood that thing about 'line colour'. I thought
you meant 'row colour' :s. Sorry about that. I'll have a look into the
matter and see whether I can come up with something (I haven't done this
before).

I'll post my findings here.

Kind regards,
 
Hi,

midgetgem said:
Ok, it still doesn't work, so I've tried emailing you. I've managed to
test my paint method on your GridTest, but this doesn't actually change
the colour of the grid line - it changes the colour of the row. Do you
know if I could change the grid line so it appears like a divider for
every block of five rows?

Thanks for your help,

Gemma

I don't think you can do it with a DataGrid (at least not easily), but I
think it can be done with DataGridView. This is a 2.0 framework class
however. It has an overridable PaintBorder method, so I assume it can be
used for what you want to do.

Kind regards,
 
Hmm, ok, i'll look into that once I get the cell backgrounds to change
colour! Will you still be able to have a look at my code for me, as I
would like to do that aswell. My paint method seemed to work with your
sample code so don't know why not with mine!

Thanks
Gemma
 
Hi Gemma,

midgetgem said:
Hmm, ok, i'll look into that once I get the cell backgrounds to change
colour! Will you still be able to have a look at my code for me, as I
would like to do that aswell. My paint method seemed to work with your
sample code so don't know why not with mine!

Thanks
Gemma

I mailed you the corrections. I forgot to mention that the mapping name of
the grid table style should be set to the name of the table in the data
source.

You could've had more freedom in looks of your game if you would paint
things yourself. Then you wouldn't have to worry about a control not being
able to display things like you want.

Good luck with your game ;-)

Kind regards,
 
That's great thanks!

Do you know how to clear the column style collection, when I click on a
new puzzle, it seems to still remember them from last time!

Cheers

Gemma
 
Back
Top