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

Get Source GridView from RowElement

4 Answers 151 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Akhil Raj
Top achievements
Rank 1
Akhil Raj asked on 21 Sep 2010, 03:55 PM
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

4 Answers, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 23 Sep 2010, 10:03 PM
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
0
Accepted
erwin
Top achievements
Rank 1
Veteran
Iron
answered on 24 Sep 2010, 09:30 AM
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



0
Jack
Telerik team
answered on 27 Sep 2010, 01:24 PM
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
0
Akhil Raj
Top achievements
Rank 1
answered on 30 Sep 2010, 07:31 AM
jk thanks admin i try this...this is simple one
Tags
GridView
Asked by
Akhil Raj
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
erwin
Top achievements
Rank 1
Veteran
Iron
Jack
Telerik team
Akhil Raj
Top achievements
Rank 1
Share this question
or