How to set different view with color to different cell in dataGrid

Aug 5, 2009 at 6:40 AM

Hi

I adding new row to dataGrid like this:

DataRow dr = dataTable.NewRow();

dr[0] = (this.dataTable.Rows.Count + 1).ToString();

dr[2] = "B";

dataTable.Rows.Add(dr);

 

and I have a problem with adding view.

dr[0].view don't exist. I tried:

SourceGrid.Cells.Views.Cell view;

int indexAA = 1;
          
            SourceGrid.Cells.ICellVirtual[] tCells = this.dataGrid.GetCellsAtRow(indexAA);
            indexAA++;

//if (pType == MessageType.Ok)
            //{
            //    view = new SourceGrid.Cells.Views.Cell();
            //    view.BackColor = Color.LightSeaGreen;
            //    ((SourceGrid.Cells.DataGrid.Cell)tCells[1]).View = view;
            //}
            //else if (pType == MessageType.Warnings)
            //{
            //    view = new SourceGrid.Cells.Views.Cell();
            //    view.BackColor = Color.LightSteelBlue;
            //    ((SourceGrid.Cells.DataGrid.Cell)tCells[2]).View = view;
            //}
            //else

....

it works, but these sample set one color to all cells in particular column. I want set for example 2 rows background red, next 5 rows background blue etc.

In common grid this is no problem : this.grid[col,row].view  = view; but in dataGrid I can access only to columns: this.dataGrid.columns[0].view = view, but result is the same like in code above.

 

Thanks for reply

 

Aug 5, 2009 at 12:32 PM
Hey Robert,
 
You may want to look at the example projects. In short, when you want to reference a new cell, you need to do a new on it, if you want that cell to have a view, you need to create a new view.
 
If you have 1000 cells, all with different colours, you need 1000 views. You cannot refererence cell.view until you do a cell.view = new View ...
 
eg:
var someView=new Cell() // This comes from the Sourcegrid.Cells.Views assembly
someView.BackColor=Color.Pink;
var editor = new TextBox(typeof (string));
grid1[row,col]=new Cells.Cell();
grid1[row,col].View=someView;
grid1[row,col].View=editor;
// You can also add your default editor here as well...
 
Make sure your editor is declared once (outside of your loop) then your view is newed for EVERY CELL. otherwise, if you change the colour of one cell, all cells will change when they repaint. If you only ever have 10 different colours, it is more efficient if you save a list of views and pick the right one  ...
 
Hope that helps,
Derek
On Wed, Aug 5, 2009 at 1:40 AM, robert_b <notifications@codeplex.com> wrote:

From: robert_b

Hi

I adding new row to dataGrid like this:

DataRow dr = dataTable.NewRow();

dr[0] = (this.dataTable.Rows.Count + 1).ToString();

dr[2] = "B";

dataTable.Rows.Add(dr);

 

and I have a problem with adding view.

dr[0].view don't exist. I tried:

SourceGrid.Cells.Views.Cell view;

int indexAA = 1;
          
            SourceGrid.Cells.ICellVirtual[] tCells = this.dataGrid.GetCellsAtRow(indexAA);
            indexAA++;

//if (pType == MessageType.Ok)
            //{
            //    view = new SourceGrid.Cells.Views.Cell();
            //    view.BackColor = Color.LightSeaGreen;
            //    ((SourceGrid.Cells.DataGrid.Cell)tCells[1]).View = view;
            //}
            //else if (pType == MessageType.Warnings)
            //{
            //    view = new SourceGrid.Cells.Views.Cell();
            //    view.BackColor = Color.LightSteelBlue;
            //    ((SourceGrid.Cells.DataGrid.Cell)tCells[2]).View = view;
            //}
            //else

....

it works, but these sample set one color to all cells in particular column. I want set for example 2 rows background red, next 5 rows background blue etc.

In common grid this is no problem : this.grid[col,row].view  = view; but in dataGrid I can access only to columns: this.dataGrid.columns[0].view = view, but result is the same like in code above.

 

Thanks for reply

 

Read the full discussion online.

To add a post to this discussion, reply to this email (sourcegrid@discussions.codeplex.com)

To start a new discussion for this project, email sourcegrid@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Feb 21, 2011 at 7:50 PM

Has there been any progress on this one.  Is it possible to change the view of an individual cell within a datagrid?  I kind of need this for my application and it would be a shame to have to use another control.

Coordinator
Feb 22, 2011 at 8:11 AM

DataGrid is special type of grid designed for performance. It can handle 1 million rows and more.  This requires some special handling of views and specifically row objects.

Unlike Grid, DataGrid does not create a Cell object for each cell in the grid. What it does is actually reusing the same cell reference across many rows and many cells. So if you need to colour some cells differently, you must create condition object, which will assign different view for a cell on specific condition.

Look at example 29, line 227, there is a AlternateView condition created, which makes every second row to have a different view. You will have to create your own view, where your specific cell would get a different view.

Feb 22, 2011 at 7:07 PM

Thanks, but I have already looked at those examples.  I completely understand the reasoning behind this design decision and I have run the sample which loads over 1million records quite quickly.  It would, however be nice to have something in between the standard grid (most flexible) and the datagrid (least flexible but highest performance).  Perhaps I will throw something together and submit a patch to someone with commit access to this project's SVN or CVS repo.

Coordinator
Feb 23, 2011 at 6:56 AM

Project repository is mercurial:   https://bitbucket.org/dariusdamalakas/sourcegrid   I am the current maintainer of this project, so you are welcome to try your ideas. A pull request would be ideal.

Could you share your ideas on how your in-between solution could work? I admit I don't see any solutions which would mix these two separate ideas.