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

change header cell based on current cell

5 Answers 32 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Missing User
Missing User asked on 27 Feb 2016, 02:19 PM

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

Sort by
0
Missing User
answered on 27 Feb 2016, 03:39 PM

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;
               }
           }

0
Missing User
answered on 27 Feb 2016, 03:41 PM

I know...kinda like killing a fly with a large baseball bat, but even worse.

But it DOES work and lets me move on for now...looking forward to the real answer.

Joe

0
Missing User
answered on 27 Feb 2016, 03:55 PM

Here is a short video of it in actions:

 

http://screencast.com/t/xjntfMn1rpQ

 

0
Missing User
answered on 29 Feb 2016, 09:07 PM

Hello --  

 

Anybody have some help on this?

Joe

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 02 Mar 2016, 08:53 AM
Hello Joe,

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.
 
Regards,
Dess
Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
Missing User
Answers by
Missing User
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or