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

Change selected grid view row color.

11 Answers 842 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Raymond
Top achievements
Rank 1
Raymond asked on 05 Nov 2010, 11:17 AM

Hi,

Could you please provide me any solution how to change selected grid view row color using Visual Style Builder.

I'd like to customize it and don't know how to handle this problem.

I was browsing it for a long time with out any result ... 

Thanks in advance,

Regards.

11 Answers, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 05 Nov 2010, 01:58 PM
Hello Raymond,

If you just want to do this using the Style builder please ask this question here.
But you can achieve the same thing programmatically by handling the RowFormatting event.
Please take a look at this example:
void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
{
    if (e.RowElement.IsSelected)
    {
        e.RowElement.BackColor = Color.Red;
    }
    else
    {
        e.RowElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
    }
}

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

Best Regards,
Emanuel Varga
0
Ram
Top achievements
Rank 1
answered on 21 Jun 2011, 08:32 AM
Hi
Every one I'm new to telerik Controls.
I'm  using RadGrid .
But where can I see the Rowformatting event in my grid.I did't find any such event in properties.
Please help me out.
I want to set the selected item forecolor.
Please help me how to handle Rowformating and Cellformating events.
0
Ivan Petrov
Telerik team
answered on 24 Jun 2011, 09:45 AM
Hello Ram,

Thank you for writing.

The solution given by Emanuel is the right approach in this scenario. In regards to your second issue I tested your current version of the controls (2010 Q2 SP2) and both CellFormatting and RowFormatting can be found in the events tab of the VS property grid when you select a RadGridView. The other way to subscribe to the events is by code as in the following code snippet:

this.radGridView1.CellFormatting += new CellFormattingEventHandler(radGridView1_CellFormatting);
this.radGridView1.RowFormatting += new RowFormattingEventHandler(radGridView1_RowFormatting);

I hope this will help. If you have further questions, feel free to ask.

Regards,
Ivan Petrov
the Telerik team
Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
0
Kamelia
Top achievements
Rank 1
answered on 23 Sep 2014, 02:39 PM
Hi , could you help me please?
I want the color of any rows which was selected before., changes 
in a way that by looking at my gridview , I can see the which of the rows has been selected ones before!
thank you 
0
Stefan
Telerik team
answered on 24 Sep 2014, 08:11 AM
Hi Kamelia,

Thank you for writing.

You can use the CurrentRowChanged event of RadGridView in order to detect when a row is being selected and store this information somewhere (for example in the row's Tag). Then, use this information in the RowFormatting event to determine whether to colorize the row or not. Here is an example:
void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
{
    if (e.RowElement.RowInfo.Tag == "Selected")
    {
        e.RowElement.BackColor = Color.Lime;
        e.RowElement.DrawFill = true;
    }
    else
    {
        e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
        e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
    }  
}
 
void radGridView1_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
{
    e.CurrentRow.Tag = "Selected";
}

I hope this helps.

Regards,
Stefan
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
Kamelia
Top achievements
Rank 1
answered on 29 Sep 2014, 11:30 AM
Hi Stefan,
Thank you for replying.

it worked. I have another question here:
whenever the gridview is binding , at the very begining , the first row of it is in selected mode!
I do not want it to be selected , I used this code in the page load:
 radGridView1.Rows[0].IsSelected = false;

but  the lime color that you had given to the selected rows , is on the first row yet!
could you help me with that please?

Thank you in advance.


0
Stefan
Telerik team
answered on 30 Sep 2014, 06:59 AM
Hi Kamelia,

Besides removing the selected row, you should remove the current row as well:
radGridView1.CurrentRow = null;

Off topic, may I please ask you to separate the questions that are not related in separate thread, so the forums remain easy to search and navigate. Thank you for the understanding.

Regards,
Stefan
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
Kamelia
Top achievements
Rank 1
answered on 30 Sep 2014, 07:46 AM
Hi again
firstly, I have to apologize for not knowing the rules correctly
I thought my question is related , so I asked it here.

secondly, I add  "radGridView1.CurrentRow = null;  "

but it makes an error in :

private void grdKartablDaftar_SelectionChanged(object sender, EventArgs e)
        {
             GridViewRowInfo ri = grdKartablDaftar.CurrentRow;
            int rowIndex = ri.Index;
            if (rowIndex >= 0)
            {
                bool selectedRowState = grdKartablDaftar.Rows[rowIndex].IsSelected;
                
            }



and the error is in the secend line :  int rowIndex = ri.Index;

: "Object reference not set to an instant f an object"
            
0
Stefan
Telerik team
answered on 30 Sep 2014, 08:30 AM
Hi,

Well, when we set the current row to null in order to clear it, if is understandable that the CurrentRow property will return null, hence the null reference exception. 

By the way, what is the purpose of this code that you pasted?

Regards,
Stefan
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
Sebastian
Top achievements
Rank 1
answered on 22 Oct 2014, 07:08 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
Stefan
Telerik team
answered on 22 Oct 2014, 08:35 AM
Hello Sebek,

To do this in code, you can use the MouseMove event of RadGridView. Here is an example:
GridCellElement lastHoveredCell;
 void radGridView1_MouseMove(object sender, MouseEventArgs e)
 {
     GridCellElement hoveredCell = radGridView1.ElementTree.GetElementAtPoint(e.Location) as GridCellElement;
 
     if (hoveredCell != null)
     {
         if (hoveredCell.RowElement.IsCurrent)
         {
             hoveredCell.RowElement.DrawFill = true;
             hoveredCell.RowElement.BackColor = Color.Yellow;
             hoveredCell.RowElement.GradientStyle = GradientStyles.Solid;
         }
         else if (lastHoveredCell != null)
         {
             lastHoveredCell.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
             lastHoveredCell.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
             lastHoveredCell.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
         }
 
         lastHoveredCell = hoveredCell;
     }
 }

I hope this helps.

Regards,
Stefan
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.

 
Tags
GridView
Asked by
Raymond
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Ram
Top achievements
Rank 1
Ivan Petrov
Telerik team
Kamelia
Top achievements
Rank 1
Stefan
Telerik team
Sebastian
Top achievements
Rank 1
Share this question
or