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
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.
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
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 Object, ByVal 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.
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~
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
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
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);
}
Doug
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)"
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