Programmatically change row color in procedure or function

10 posts, 1 answers
  1. Kevin Weir
    Kevin Weir avatar
    2 posts
    Member since:
    Jun 2009

    Posted 30 Mar 2011 Link to this post

     

    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

     

  2. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 31 Mar 2011 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jonald
    Jonald avatar
    2 posts
    Member since:
    Oct 2014

    Posted 01 Oct 2014 in reply to Emanuel Varga Link to this post

    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

  5. Dess
    Admin
    Dess avatar
    1605 posts

    Posted 06 Oct 2014 Link to this post

    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.
     
  6. Jonald
    Jonald avatar
    2 posts
    Member since:
    Oct 2014

    Posted 06 Oct 2014 in reply to Dess Link to this post

    Thank you for the wonderful explanation. You're right, I'd figured out also that you should reset all the elements. :)
  7. Sebastian
    Sebastian avatar
    4 posts
    Member since:
    Mar 2014

    Posted 22 Oct 2014 in reply to Dess Link to this post

    I have not found anywhere the way to change row's background color when mouse is over the row...
    Is there any way?
  8. Dess
    Admin
    Dess avatar
    1605 posts

    Posted 24 Oct 2014 Link to this post

    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.

     
  9. Shakti SIngh Dulawat
    Shakti SIngh Dulawat avatar
    89 posts
    Member since:
    Oct 2009

    Posted 05 Aug Link to this post

    Desislava  Wonderful work
  10. Victor
    Victor avatar
    2 posts
    Member since:
    Mar 2016

    Posted 2 days and 19 hours ago Link to this post

    How to change the color of a particular row that is not the multicolumn selection in the ComboBox
  11. Dess
    Admin
    Dess avatar
    1605 posts

    Posted 1 hour ago Link to this post

    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.
Back to Top
UI for WinForms is Visual Studio 2017 Ready