This is a migrated thread and some comments may be shown as answers.

Set Various Colors in RadGridView

10 Answers 500 Views
GridView
This is a migrated thread and some comments may be shown as answers.
vighnesh
Top achievements
Rank 1
vighnesh asked on 20 Feb 2009, 07:11 AM
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.

10 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 23 Feb 2009, 10:40 AM
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.
0
luis
Top achievements
Rank 1
answered on 25 Mar 2009, 04:48 PM
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

 

0
Jack
Telerik team
answered on 25 Mar 2009, 07:11 PM
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.
0
Kirk Roberts
Top achievements
Rank 1
answered on 01 Oct 2010, 12:09 AM
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~
0
Emanuel Varga
Top achievements
Rank 1
answered on 03 Oct 2010, 11:56 PM
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

0
Jack
Telerik team
answered on 06 Oct 2010, 01:15 PM
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
0
Kirk Roberts
Top achievements
Rank 1
answered on 06 Oct 2010, 03:21 PM

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);

 

 

 

 

}

0
Doug
Top achievements
Rank 1
answered on 29 Nov 2012, 10:23 AM
Great answer and great code example, Jack. Very clear reply. Thank you.
Doug
0
Daniel
Top achievements
Rank 1
answered on 12 Aug 2014, 07:48 AM
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)"
0
Stefan
Telerik team
answered on 12 Aug 2014, 01:17 PM
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.
 
Tags
GridView
Asked by
vighnesh
Top achievements
Rank 1
Answers by
Jack
Telerik team
luis
Top achievements
Rank 1
Kirk Roberts
Top achievements
Rank 1
Emanuel Varga
Top achievements
Rank 1
Doug
Top achievements
Rank 1
Daniel
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or