I have tried all sorts of things and I can get this to *almost* work.
I have a grid with "stacked" data entry columns, as such it can be a little tricky to see which one you are in.
I am wanting to highlight the header column for whichever column I am in, sort of like Excel does.
I can change the individual header colors through checking:
if (e.CellElement is GridHeaderCellElement || e.CellElement is GridGroupContentCellElement)
{
if (e.CellElement.ColumnInfo.Name == "podName")
But I cannot seem to get it to work just when I am in that cell.
I have tried setting a variable telling me when the cell is selected by doing something like:
private void radGridView1_CurrentCellChanged(object sender, CurrentCellChangedEventArgs e)
{
var test = e.NewCell;
if (test.ColumnInfo.Name == "podName")
{
//currentcolor = System.Drawing.Color.Black;
currentCellIs = 1;
But that does not work correct either.
Has anybody else done this or have any suggestions?
Thank you,
Joe
5 Answers, 1 is accepted
I have the below code working, but there has GOT to be a better way...
Whenever a cell change event happens, check it if is one of our target cells, if so change the first character of the headertext:
private
void
radGridView1_CurrentCellChanged(
object
sender, CurrentCellChangedEventArgs e)
{
var test = e.NewCell;
if
(test.ColumnInfo.Name ==
"podName"
)
{
this
.radGridView1.Columns[
"podName"
].HeaderText =
"*Name"
;
this
.radGridView1.Columns[
"podTime"
].HeaderText =
"Time"
;
this
.radGridView1.Columns[
"podDate"
].HeaderText =
"Date"
;
this
.radGridView1.Columns[
"podQty"
].HeaderText =
"Qty"
;
}
else
{
this
.radGridView1.Columns[
"podName"
].HeaderText =
"Name"
;
}
if
(test.ColumnInfo.Name ==
"podTime"
)
{
this
.radGridView1.Columns[
"podName"
].HeaderText =
"Name"
;
this
.radGridView1.Columns[
"podTime"
].HeaderText =
"*Time"
;
this
.radGridView1.Columns[
"podDate"
].HeaderText =
"Date"
;
this
.radGridView1.Columns[
"podQty"
].HeaderText =
"Qty"
;
}
else
{
this
.radGridView1.Columns[
"podTime"
].HeaderText =
"Time"
;
}
if
(test.ColumnInfo.Name ==
"podDate"
)
{
this
.radGridView1.Columns[
"podName"
].HeaderText =
"Name"
;
this
.radGridView1.Columns[
"podTime"
].HeaderText =
"Time"
;
this
.radGridView1.Columns[
"podDate"
].HeaderText =
"*Date"
;
this
.radGridView1.Columns[
"podQty"
].HeaderText =
"Qty"
;
}
else
{
this
.radGridView1.Columns[
"podDate"
].HeaderText =
"Date"
;
}
if
(test.ColumnInfo.Name ==
"podQty"
)
{
this
.radGridView1.Columns[
"podName"
].HeaderText =
"Name"
;
this
.radGridView1.Columns[
"podTime"
].HeaderText =
"Time"
;
this
.radGridView1.Columns[
"podDate"
].HeaderText =
"Date"
;
this
.radGridView1.Columns[
"podQty"
].HeaderText =
"*Qty"
;
}
else
{
this
.radGridView1.Columns[
"podQty"
].HeaderText =
"Qty"
;
}
}
then in the cell formatting event look if the first character of the cell is a "*"
private
void
radGridView1_ViewCellFormatting(
object
sender, CellFormattingEventArgs e)
{
if
(e.CellElement
is
GridHeaderCellElement)
{
try
{
//var test = e.Column.HeaderText.ToString();
var test = e.Column.HeaderText.ToString().Substring(0, 1);
if
(test ==
"*"
)
{
e.CellElement.DrawBorder =
true
;
e.CellElement.DrawFill =
true
;
e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
e.CellElement.BackColor = Color.White;
// e.CellElement.ForeColor = Color.Red;
}
else
{
e.CellElement.ResetValue(LightVisualElement.DrawBorderProperty, ValueResetFlags.Local);
e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
e.CellElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
}
}
catch
(Exception)
{
//throw;
}
}
Thank you for writing.
You can easily achieve the desired formatting by only using the ViewCellFormatting event. Please refer to the following code snippet:
private
void
radGridView1_ViewCellFormatting(
object
sender, CellFormattingEventArgs e)
{
if
(e.CellElement
is
GridHeaderCellElement && e.Column.IsCurrent)
{
e.CellElement.BackColor = Color.White;
e.CellElement.DrawFill =
true
;
e.CellElement.ForeColor = Color.Aqua;
e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
}
else
{
e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
e.CellElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
}
}
I hope this information helps. Should you have further questions I would be glad to help.
Dess
Telerik