This is a migrated thread and some comments may be shown as answers.
Programmatically change row color in procedure or function
9 Answers 233 Views
This is a migrated thread and some comments may be shown as answers.
Kevin Weir
Top achievements
Rank 1
Kevin Weir asked on 31 Mar 2011, 01:03 AM

 

I currently have a save button click event that calls a procedure named ValidateData.   The ValidateData procedure iterates through rows in the grid and sets the color or the row to red if the data therin is invalid.   The problem I have is that after upgrading to RadControls 2011 Q1 the VisualElement property is no longer available.  I have been searching through these forums for a couple of hours and haven't run across a viable way of dealing with this.    Is there an easy way to accomplish this?  I don't believe the RowFormatting Event applies in my case.

Much appreciated
Kevin



Private
Function ValidateData() As Boolean

 

 

Dim entBundleComponent As BundleComponent

 

 

 

Dim isValid As Boolean = True

 

 

Dim isValidRow As Boolean = True

 

 

Dim gvRow As UI.GridViewRowInfo

 

 

 

For Each gvRow In gvComponents.Rows

 

 

isValidRow =

True

 

entBundleComponent =

CType(gvRow.DataBoundItem, BundleComponent)

 

 

 

If entBundleComponent.COMPONENT_NAME.Trim().Length = 0 Then

 

isValid =

False

 

isValidRow =

False

 

 

End If

 

 

If entBundleComponent.COMPONENT_DESCRIPTION.Trim().Length = 0 Then

 

isValid =

False

 

isValidRow =

False

 

 

End If

 

 

If isValidRow Then

 

gvRow.VisualElement.BackColor = Color.Transparent

gvRow.VisualElement.DrawFill =

True

 

 

Else

 

gvRow.VisualElement.BackColor = Color.Red

gvRow.VisualElement.DrawFill =

True

 

 

End If

 

 

Next

 

 

If isValid Then

 

txtMessages.Text =

"Ok"

 

txtMessages.ForeColor = Color.Black

 

Else

 

txtMessages.ForeColor = Color.Red

txtMessages.Text =

"Please correct errors before saving."

 

 

End If

 

 

Return isValid

 

 

 

End Function

 

9 Answers, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 31 Mar 2011, 08:29 AM
Hello Kevin,

You should use RowFormatting for this one, something like this:
Private Sub RadGridView1_RowFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.RowFormattingEventArgs) Handles RadGridView1.RowFormatting
    'check for null databound item
    entBundleComponent = CType(gvRow.DataBoundItem, BundleComponent)
 
    If entBundleComponent.COMPONENT_NAME.Trim().Length = 0 Then
        e.RowElement.DrawFill = True
        e.RowElement.GradientStyle = GradientStyles.Solid
        e.RowElement.BackColor = Color.Red
    Else
        e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local)
        e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local)
        e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local)
    End If
End Sub

For more info please take a look at the following help article.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
Jonald
Top achievements
Rank 1
answered on 01 Oct 2014, 11:10 PM
Hi sir, how about if the values is on the list or in my listbox, how can i change the backcolor of my telerik gridview? here is my code:


for i = 0 to listbox2.rows.count -1
 If e.RowElement.RowInfo.Cells(0).Value = CIint(listbox2.items(i)) Then
                     
            e.RowElement.DrawFill = True
            e.RowElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid
            e.RowElement.BackColor = Color.Red
     
        Else

            e.RowElement.DrawFill = False

        End If
Next

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 06 Oct 2014, 02:21 PM
Hello Jonald,

Thank you for writing.

Due to the UI virtualization in RadGridView, cell/row elements are created only for currently visible cells/rows and are being reused during operations like scrolling, filtering, grouping and so on.
In order to prevent applying the formatting to other columns' cell/row elements (because of the cell reuse) all customization should be reset for the rest of the cell/row elements. It is recommended to reset not only the DrawFill property, but the all the properties you set, including BackColor and the GradientStyle properties. You can refer to our Formatting Cells and Formatting Rows help articles which are quite useful about this topic.

I am not sure why you are iterating through all the items in the list box for a specific row in the RowFormatting event, but if a certain list box item is equal to the cell's value and you apply a specific style to the cell, the next item would not be the same and you will reset the applied settings. You should either use break to stop the iteration when a match is found or use another approach for using the list box data inside the RowFormatting event handler. You can find below a sample code snippet demonstrating how to change the row's back color for the currently selected item in the list box:
public Form1()
{
    InitializeComponent();
    List<Item> items = new List<Item>();
    for (int i = 0; i < 30; i++)
    {
        items.Add(new Item(i,"Item" + i));
    }
 
    this.radGridView1.DataSource = items;
    this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    this.listBox1.DataSource = items;
    this.listBox1.DisplayMember = "Title";
    this.listBox1.ValueMember = "Id";
}
 
public class Item
{
    public int Id { get; set; }
 
    public string Title { get; set; }
 
    public Item(int id, string title)
    {
        this.Id = id;
        this.Title = title;
    }
}
 
private void radGridView1_RowFormatting(object sender, Telerik.WinControls.UI.RowFormattingEventArgs e)
{
    if ((int)e.RowElement.RowInfo.Cells[0].Value == ((Item)listBox1.SelectedItem).Id)
    {
        e.RowElement.DrawFill = true;
        e.RowElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
        e.RowElement.BackColor = Color.Red;
    }
    else
    {
        e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
        e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
        e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
    }
}


I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
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.
 
0
Jonald
Top achievements
Rank 1
answered on 07 Oct 2014, 01:16 AM
Thank you for the wonderful explanation. You're right, I'd figured out also that you should reset all the elements. :)
0
Sebastian
Top achievements
Rank 1
answered on 22 Oct 2014, 07:06 AM
I have not found anywhere the way to change row's background color when mouse is over the row...
Is there any way?
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 24 Oct 2014, 03:46 PM
Hello Sebastian,

Thank you for writing.

Your question has already been answered in this forum thread. Please, see our answer there for more information. We kindly ask you to use just one thread for a specific problem.

Thank you for your understanding.

Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Shakti SIngh Dulawat
Top achievements
Rank 1
answered on 05 Aug 2016, 02:36 PM
Desislava  Wonderful work
0
Victor
Top achievements
Rank 1
answered on 02 Dec 2016, 03:05 PM
How to change the color of a particular row that is not the multicolumn selection in the ComboBox
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 05 Dec 2016, 09:35 AM
Hello Victor,

Thank you for writing.  

In order to customize grid cells, it is suitable to use the RadGridView.CellFormatting event: http://docs.telerik.com/devtools/winforms/gridview/cells/formatting-cells

The popup grid can be access by the EditorControl property of RadMultiColumnComboBox.

I hope this information helps. Should you have further questions I would be glad to help.

Regards,
Dess
Telerik by Progress
Telerik UI for WinForms is ready for Visual Studio 2017 RC! Learn more.
Asked by
Kevin Weir
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Jonald
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Sebastian
Top achievements
Rank 1
Shakti SIngh Dulawat
Top achievements
Rank 1
Victor
Top achievements
Rank 1
Share this question
or