Infralution Support Forum Index Infralution Support
Support groups for Infralution products
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to change the background of a whole row?

 
Post new topic   Reply to topic    Infralution Support Forum Index -> Virtual Tree Support
View previous topic :: View next topic  
Author Message
JohnSummit



Joined: 10 Apr 2008
Posts: 32

PostPosted: Sun Jul 12, 2009 4:01 am    Post subject: How to change the background of a whole row? Reply with quote

It seems the only way to change the background of cells in a row is to implement the GetCellData event and put in code similar to this:

Code:

        private void virtualTree1_GetCellData(object sender, GetCellDataEventArgs e)
        {
            // get the default binding for the given row and use it to populate the cell data
            RowBinding binding = virtualTree1.GetRowBinding(e.Row);
            binding.GetCellData(e.Row, e.Column, e.CellData);

            if (e.Row.Item is SomeType)
            {
                e.CellData.OddStyle.BackColor = Color.BlueViolet;
                e.CellData.EvenStyle.BackColor = Color.BlueViolet;
            }
        }


This, however, doesn't change the color of the whole row, only the cells background. Is there a way to change the background similar to how the current row is marked?
Back to top
View user's profile Send private message
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Sun Jul 12, 2009 11:42 pm    Post subject: Reply with quote

To change the whole row color you need to handle both the GetRowData and the GetCellData methods. Note that you should not just set the BackColor of the existing style - since this will affect all rows/cells that use the current style. Instead you need to create a new derived style. See the following FAQ on this topic:

http://www.infralution.com/phpBB2/viewtopic.php?t=419
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
JohnSummit



Joined: 10 Apr 2008
Posts: 32

PostPosted: Mon Jul 13, 2009 12:07 am    Post subject: Reply with quote

Thank you very much. Works well.
Back to top
View user's profile Send private message
JohnSummit



Joined: 10 Apr 2008
Posts: 32

PostPosted: Mon Jul 13, 2009 4:09 am    Post subject: String alignment lost when applying copy of cell/row style. Reply with quote

I'm noticing that the string alignment is lost when applying a copy of cell/row style. This is the code i'm using to copy the cell style and change the background to a shade of yellow:

Code:

private void virtualTree1_GetCellData(object sender, GetCellDataEventArgs e)
        {
            //////////////////////////////////////////
            // Marker hi-lighting code.
            //////////////////////////////////////////

            TreeNodeObject treeNodeObject = e.Row.Item as TreeNodeObject;
            if (treeNodeObject.Marker != null)
            {
                if (m_markerCellDataStyle == null)
                {
                    m_markerCellDataStyle = new Infralution.Controls.Style(e.CellData.OddStyle);
                    m_markerCellDataStyle.BackColor = Color.LightYellow;
                }

                e.CellData.EvenStyle = m_markerCellDataStyle;
                e.CellData.OddStyle = m_markerCellDataStyle;
            }
        }


The same code is used for the row stuff:

Code:

        private void virtualTree1_GetRowData(object sender, GetRowDataEventArgs e)
        {
            //////////////////////////////////////////
            // Marker hi-lighting code.
            //////////////////////////////////////////

            TreeNodeObject treeNodeObject = e.Row.Item as TreeNodeObject;
            if (treeNodeObject.Marker != null)
            {
                if (m_markerRowDataStyle == null)
                {
                    m_markerRowDataStyle = new Infralution.Controls.Style(e.RowData.OddStyle);
                    m_markerRowDataStyle.BackColor = Color.LightYellow;
                }

                e.RowData.EvenStyle = m_markerRowDataStyle;
                e.RowData.OddStyle = m_markerRowDataStyle;
            }
        }


One of my cells has the string alignment centered but is aligned left (near) after the new style is assigned. Could you tell me where to re-apply the string alignment?

Thanks.
Back to top
View user's profile Send private message
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Mon Jul 13, 2009 4:43 am    Post subject: Reply with quote

I assume that your code is still calling

Code:
RowBinding binding = virtualTree1.GetRowBinding(e.Row);
binding.GetCellData(e.Row, e.Column, e.CellData);


but you have omitted that in your post?

I think the problem is that you are only creating m_MarkerCellDataStyle once - however this won't work if the original parent cell styles are different - since you will end up with all cells formatted with the one style.

In this case you probably need to bite the bullet and simply create a new style for each cell ie:

Code:
Style style = new Style(e.CellData.OddStyle);
style.BackColor = Color.LightYellow;
e.CellData.OddStyle = style;
e.CellData.EvenStyle = style;


This isn't as bad on performance as you might think because .NET memory management is very efficient when it comes to allocating new objects.
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
JohnSummit



Joined: 10 Apr 2008
Posts: 32

PostPosted: Tue Jul 14, 2009 2:31 am    Post subject: Reply with quote

Thanks. I ended up creating a new style for each row / cell and the text alignment is now preserved.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Infralution Support Forum Index -> Virtual Tree Support All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group