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
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
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.
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
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
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
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.
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
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"
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
Is there any way?
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.