Get Source GridView from RowElement

5 posts, 2 answers
  1. Akhil Raj
    Akhil Raj avatar
    69 posts
    Member since:
    Aug 2012

    Posted 21 Sep 2010 Link to this post

    hi ,
       i am using RAdGridview in my windows form. I implemented forcolor change in rowformatting event. I have 2 gridview in my form. So i just think to generalize the row formatting to work both gridview. But according to gridview name i just change the forcolor. How can i get gridview name in the rowformatting event
  2. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 23 Sep 2010 Link to this post

    Hello Akhil,

    I have prepared here two options in order to get the row sender, one is uglier but more precise and the other one is cleaner but it is slower, you can chose for yourself.

    void radGridView1_RowFormatting(object sender, Telerik.WinControls.UI.RowFormattingEventArgs e)
            {
                // 1. Going trough the hierarchy from the rowElement up to the grid
                var grid =
                    ((Telerik.WinControls.UI.GridVisualElement)(e.RowElement.Parent.Parent.Parent.Parent.Parent)).
                        GridControl;
     
                //2. Searching in the rows collection for a grid if it has that rowInfo
                if (radGridView1.Rows.Contains(e.RowElement.RowInfo))
                {
                    //this is the grid
                }
                else
                {
                    //this is the grid
                }
            }

    Hope this answers your question,
    Best Regards,
    Emanuel Varga
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    erwin
    erwin avatar
    358 posts
    Member since:
    Dec 2006

    Posted 24 Sep 2010 Link to this post

    Akhil,

    I think registering the same event handler with two (or more) grids leads to hard-to-maintain code. I would factor the action out into   a function and call it from two event handlers, something like this:

    void radGridView1_RowFormatting(object sender, Telerik.WinControls.UI.RowFormattingEventArgs e)
    {
            ChangeCellForeColor(radGridView1, e, Color.Red)
    }
    void radGridView2_RowFormatting(object sender, Telerik.WinControls.UI.RowFormattingEventArgs e)
    {
            ChangeCellForeColor(radGridView2, e, Color.Black)
    }

    void ChangeCellForeColor(RadGridView grid, Telerik.WinControls.UI.RowFormattingEventArgs e,Color col)
    {
            .....
    }

    Regards
    Erwin



  5. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 27 Sep 2010 Link to this post

    Emanuel, thank you for your suggestion.

    Erwin, I agree. It will be much simpler to use two different events. 

    Akhil, you can do this by using the GridControl property of GridRowElement which is accessible through the event sender. Please consider the sample below:
    void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
    {
        GridRowElement row = (GridRowElement)sender;
        RadGridView grid = row.GridControl;
        string name = grid.Name;
        //...
    }

    I hope this helps.

    Greetings,
    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
  6. Akhil Raj
    Akhil Raj avatar
    69 posts
    Member since:
    Aug 2012

    Posted 30 Sep 2010 Link to this post

    jk thanks admin i try this...this is simple one
Back to Top
UI for WinForms is Visual Studio 2017 Ready