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

RowElement - get row index

8 Answers 972 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ryan
Top achievements
Rank 1
Ryan asked on 14 Oct 2009, 02:43 PM
I have a situation that I need to change the nth row's background color.  I am using the RowFormatting event as recommended.  However, I'm struggling to find the row index number. 

void tblSearchSSN_RowFormatting(object sender, RowFormattingEventArgs e)
        {
            if (e.....ROW_NUMBER = 5)
               {
                       ....do stuff....
               }
         }



This is probably a really easy one, but any help would be great! 
Thanks Ryan

8 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 15 Oct 2009, 09:04 AM
Hi Ryan,

You can obtain the row index by using Rows.IndexOf method. However, RowFormatting event fires frequently and this is a slow method. So, I suggest using a hidden column that contains the row index:

void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
{
    int rowIndex = this.radGridView1.Rows.IndexOf(e.RowElement.RowInfo); // this is slow operation
    int rowIndex = (int)e.RowElement.RowInfo.Cells["RowIndex"].Value;
    //...
}


Regards,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Ryan
Top achievements
Rank 1
answered on 16 Oct 2009, 03:09 PM
Thank you for the quick response, does shed some light on things.  Maybe a better question is, what's the best way to set an entire row background color to a certain color.

Everything I read said to do it in the RowFormatting.  Basically what I want to do is if a certain condition exists in the row, set the color.  So if the third cell = "Y", set background of just that row to say light blue?  Is there a better way to do this?  When in the RowFormatting event, how can you get the cell value of a specific column if you don't know the row number (or have to use the slow method in which you talked about.  Is there anything you recommend?

Thanks again for everything.  Ryan
0
Nikolay
Telerik team
answered on 22 Oct 2009, 04:42 PM
Hi Ryan,

Since you have such specific condtions (checking a cell for a value) you do not need to get the row index.
Please refer to the following code snippet that will help you in your case:
void radGridView1_RowFormatting(object sender, Telerik.WinControls.UI.RowFormattingEventArgs e)
{
    if (e.RowElement.RowInfo.Cells[2].Value.ToString() == "Y")
    {
        e.RowElement.DrawFill = true;
        e.RowElement.BackColor = Color.Red;
    }
    else
    {
        if (e.RowElement.BackColor == Color.Red)
        {
            e.RowElement.ResetValue(VisualElement.BackColorProperty);
            e.RowElement.ResetValue(LightVisualElement.DrawFillProperty);
        }
    }
}

I hope this helps. If you have additional questions, feel free to contact me.

Greetings,
Nikolay
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Ryan
Top achievements
Rank 1
answered on 12 Nov 2009, 01:44 PM
Thank you for the help, sorry for the delay in getting back with you.  I got called off to another job.

It works except after the table is populated, when I scroll through, as I scroll, now every cell for that column changes to blue.  Initially only correct ones are blue font, but after scrolling to the bottom, all of them are blue font, even the one's that shouldn't.  Can someone explain this behavior and maybe a way around it?

void tblSearchName_CellFormatting(object sender, CellFormattingEventArgs e)
        {
           if (e.CellElement.ColumnInfo.HeaderText == "Case Number")
           {
               if (Convert.ToString(e.CellElement.RowInfo.Cells["colWaiver"].Value) != string.Empty)
               {
                   e.CellElement.DrawFill = true;
                   e.CellElement.ForeColor = Color.Blue;
               }
               else
               {
                   if (e.CellElement.BackColor == Color.Blue)
                   {
                       e.CellElement.ResetValue(VisualElement.BackColorProperty);
                       e.CellElement.ResetValue(LightVisualElement.DrawFillProperty);
                   }

               }
           }
           
            
        }
0
PunjabiSingh
Top achievements
Rank 2
answered on 13 Nov 2009, 05:24 AM
Hi Ryan
very simple

use this for formatting cell or row

 Dim obj As New ConditionalFormattingObject("MyCondition", ConditionTypes.Equal, "Y", "", False)
                obj.CellForeColor = Color.White
                obj.CellBackColor = Color.Green
                RadGridView.Columns(7).ConditionalFormattingObjectList.Add(obj)

if you put last False to True then it will be formatting for full row

for get index of row use this

RowIndex = RadGridView.CurrentRow.ViewTemplate.Rows.IndexOf(RadGridView.CurrentRow)

old method of getting Gridinfo.currentindex doesnt work

Kind Regards:

0
Ryan
Top achievements
Rank 1
answered on 13 Nov 2009, 01:42 PM
This is great!  Exactly what I was looking for!  A couple more questions:

a)  How do you also set the font of the row/cell to bond/italized/different size/font name...by using the obj

b)  Is there a way to test that column(2) = "Y" and then set column(3) to blue (as an example)?

Once again, thank you so much for the help!  Ryan
0
Nikolay
Telerik team
answered on 18 Nov 2009, 09:50 PM
Hello Ryan,

The answer to your questions is the CellFormatting event. For additional information please refer to the following support resources:
Formatting Cells
Changing the visual appearance of rows and cells using code

I hope this helps. If you need additional assistance, feel free to contact me.

Sincerely yours,
Nikolay
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Ryan
Top achievements
Rank 1
answered on 19 Nov 2009, 01:57 PM
This is great, once again thanks for your help!  Ryan
Tags
GridView
Asked by
Ryan
Top achievements
Rank 1
Answers by
Jack
Telerik team
Ryan
Top achievements
Rank 1
Nikolay
Telerik team
PunjabiSingh
Top achievements
Rank 2
Share this question
or