Set Various Colors in RadGridView

11 posts, 0 answers
  1. vighnesh
    vighnesh avatar
    7 posts
    Member since:
    Feb 2009

    Posted 20 Feb 2009 Link to this post

    Hello,

    There are three problems I am currently facing. Please give some ideas about this.

     First I have set the alternative color but it does not seem to be consistent i..e instead of two consecutive rows having instead color they can have same colors so Consistency is not maintained.

    Second, How do I set Header Row Captions Bold?

    Third, How do we change Row Selection Color and Row On mouse over color programmatically?

    Thank you.
  2. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 23 Feb 2009 Link to this post

    Hi vighnesh,

    Thank you for contacting us.

    Regarding your questions:

    1. I am not sure if I understand this question. Could you please describe with more details the desired behavior. Sending a screenshot or the source code of your application would be helpful.

    2. You should handle the ViewCellFormatting event to make the font bold in column headers. Here is a sample:

    Font font = new Font(SystemFonts.DialogFont, FontStyle.Bold); 
     
    void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e) 
        if (e.CellElement.RowInfo is GridViewTableHeaderRowInfo) 
        { 
            e.CellElement.Font = this.radGridView1.Font = font; 
        } 

    I should emphasize that the Font is a system resource and it should be used with care. You should dispose every Font instance. I suggest that you also use the same font instance for all cells when possible.

    3. Using the Visual Style Builder is the preferred way for customizing grid rows. However, it is possible to change mouse hover and selection colors through handling the MouseMove and the RowFormatting events. Consider the code snippet below:

    GridRowElement hoveredRow; 
     
    void radGridView1_MouseMove(object sender, MouseEventArgs e) 
        GridRowElement row = null
        RadElement element = this.radGridView1.ElementTree.GetElementAtPoint(e.Location); 
        if (element is GridRowElement) 
        { 
            row = (GridRowElement)element; 
        } 
        if (element is GridCellElement) 
        { 
            row = (GridRowElement)element.Parent; 
        } 
        if (row != null
        { 
            if (hoveredRow != null
            { 
                hoveredRow.DrawFill = true
                if (hoveredRow.IsCurrent) 
                { 
                    hoveredRow.BackColor = Color.Blue; 
                } 
                else 
                { 
                    hoveredRow.BackColor = Color.White; 
                } 
                hoveredRow.GradientStyle = GradientStyles.Solid; 
            } 
            hoveredRow = row; 
            if (hoveredRow != null
            { 
                hoveredRow.DrawFill = true
                hoveredRow.BackColor = Color.Red; 
                hoveredRow.GradientStyle = GradientStyles.Solid; 
            } 
        } 
     
    void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e) 
        if (e.RowElement.IsCurrent) 
        { 
            e.RowElement.BackColor = Color.Blue; 
        } 
        else 
        { 
            e.RowElement.BackColor = Color.White; 
        } 
        e.RowElement.DrawFill = true
        e.RowElement.GradientStyle = GradientStyles.Solid; 
     

    I hope this helps. Please do not hesitate to write us if you need further assistance.

    All the best,
    Jack
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. luis
    luis avatar
    3 posts
    Member since:
    Oct 2008

    Posted 25 Mar 2009 Link to this post

    i cant set distinct colors to distinct rows in vb.net with radgridview can u helpme?

    i tried somthing like this:

     

    Try

     

     

    If dg_painel.Rows(j).Cells(1).Value = "Produ‡Æo" Then

     

    dg_painel.Rows(j).VisualElement.BackColor = Color.Crimson

     

    End If

     

  5. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 25 Mar 2009 Link to this post

    Hello luis,

    RadGridView for WinForms uses virtualization for its rows. This mans that only the rows that are visible on the screen have visual elements and they are reused when the grid is scrolled. This improves the performance and minimizes the memory usage.

    It is safe to access grid's visual elements only inside the CellFormatting and RowFormatting events. Here is a sample on how to change the row color based on a cell value:

    Private Sub radGridView1_RowFormatting(ByVal sender As ObjectByVal e As RowFormattingEventArgs) 
        If TypeOf e.RowElement Is GridDataRowElement Then 
            If e.RowElement.RowVisualState = GridRowElement.RowVisualStates.None Then 
                If CInt(e.RowElement.RowInfo.Cells("Value1").Value) > 5 Then 
                    e.RowElement.BackColor = Color.Crimson 
                    e.RowElement.GradientStyle = GradientStyles.Solid 
                    e.RowElement.DrawFill = True 
                Else 
                    e.RowElement.DrawFill = False 
                End If 
            End If 
        End If 
    End Sub 

    I hope this helps. If you have any questions, don't hesitate to ask.

    Regards,
    Jack
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  6. Kirk Roberts
    Kirk Roberts avatar
    2 posts
    Member since:
    Oct 2008

    Posted 30 Sep 2010 Link to this post

    I am using the radGridView for 2010 Q1, my code follows:

    private

     

    void radGridView2_RowFormatting(object sender, RowFormattingEventArgs e)

     

    {

     

    try

     

    {

     

     

    if (e.RowElement.RowInfo.Cells["Accepted"].Value.ToString().Trim() == "N")

     

    {

    e.RowElement.BackColor =

    Color.Red;

     

    e.RowElement.DrawFill =

    true;

     

    }

     

    else

     

    {

    e.RowElement.BackColor =

    Color.White;

     

    e.RowElement.DrawFill =

    true;

     

     

    //e.RowElement.DrawFill = false;

     

     

    //e.RowElement.ResetValue(Telerik.WinControls.VisualElement.BackColorProperty);

     

    }

    }

     

    catch (Exception ex)

     

    {

     

    throw ex;

     

    }

    }



    The issue that I am having is, when the row is selected, by mouse clicking it, the control turns amber, which i think is default.  But once  I have clicked on another row, the previous amber row stays amber.  If i comment out all the "else" statement code (see above), the row color does indeed go back to white/transparent.   I need this code in order to turn the back color of certain rows RED.  I think the row is also going amber when I select the red rows, but since red is much darker than amber it is hardly noticable.

    Please tell me, what am I doing wrong? 

    Thank you for any help you can give.

    elKirko~
  7. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 03 Oct 2010 Link to this post

    Hello Kirk,

    The problem here is that the grid, for performance reasons, reuses cells/rows, so when you are using row / cell formatting you have to revert the reset the visual item property to the default one, so, if you need some rows to be amber, red, and so on you should do all of those tests in the RowFormatting event, something like

    If (something)
    //set color to amber
    else if (something else)
    //set color to red
    else
    // reset property here

    e.RowElement.DrawFill = false;
    e.RowElement.ResetValue(Telerik.WinControls.VisualElement.BackColorProperty)

    If you need more information about RowFormatting you could read this article (http://www.telerik.com/help/winforms/formatting_rows.html) or if you need some information about the logical and visual structure of the grid, more specifically the virtualization you could check out this article (http://www.telerik.com/help/winforms/grid_logical-vs-visual-grid-structure.html)

    Hope this helps, if you have any more questions please do not hesitate to say so,

    Best Regards,
    Emanuel Varga

  8. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 06 Oct 2010 Link to this post

    Hi Kirk Roberts,

    Thank you for contacting us.

    Your code looks OK and I was not able to reproduce the issue when using our latest release - Q2 2010 SP2. I noticed that you are using an old version of RadGridView. I recommend that you try the latest one. It contains many new features and addresses lots of issues. If the issue continues to appear, please send me your application and I will be glad to help further.

    I am looking forward to your reply.

    Sincerely yours,
    Jack
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  9. Kirk Roberts
    Kirk Roberts avatar
    2 posts
    Member since:
    Oct 2008

    Posted 06 Oct 2010 Link to this post

    First, thank you for your quick reply. 
    Here is the fix for the code.  Was the RowElement reset all along.  One of your colleagues shared this with me.

    Again, this code is for radGridView for WinForms 2010 Q1

     

    if

     

    (e.RowElement.RowInfo.Cells["Accepted"].Value == "N")

     

    {

    e.RowElement.BackColor =

    Color.Red;

     

    e.RowElement.GradientStyle = Telerik.WinControls.

    GradientStyles.Solid;

     

    e.RowElement.DrawFill =

    true;

     

    }

     

    else

     

     

     

     

    {

    e.RowElement.ResetValue(

    LightVisualElement.GradientStyleProperty, Telerik.WinControls.ValueResetFlags.Local);

     

    e.RowElement.ResetValue(

    LightVisualElement.DrawFillProperty, Telerik.WinControls.ValueResetFlags.Local);

     

    e.RowElement.ResetValue(Telerik.WinControls.

    VisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);

     

     

     

     

    }

  10. Doug
    Doug avatar
    3 posts
    Member since:
    Aug 2012

    Posted 29 Nov 2012 Link to this post

    Great answer and great code example, Jack. Very clear reply. Thank you.
    Doug
  11. Daniel
    Daniel avatar
    5 posts
    Member since:
    Mar 2011

    Posted 12 Aug 2014 in reply to Jack Link to this post

    Hi Jack,

    How about if i have an error on RowFormattingEventArgs?
    The error is :
    "The type or namespace name RowFormattingEventArgs could not be found (are you missing a using directive or an assembly reference)"
  12. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 12 Aug 2014 Link to this post

    Hello Daniel,

    The type mentioned is located in the Telerik.WinControls.GridView assembly in namespace Telerik.WinControls.UI. Please make sure you have this assembly referenced.

    I hope that you find this information useful.

    Regards,
    Stefan
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top
UI for WinForms is Visual Studio 2017 Ready