change header cell based on current cell

6 posts, 0 answers
  1.  avatar
    5 posts
    Member since:
    Feb 2016

    Posted 27 Feb Link to this post

    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

  2.  avatar
    5 posts
    Member since:
    Feb 2016

    Posted 27 Feb in reply to Link to this post

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

  3. UI for WinForms is Visual Studio 2017 Ready
  4.  avatar
    5 posts
    Member since:
    Feb 2016

    Posted 27 Feb in reply to Link to this post

    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

  5.  avatar
    5 posts
    Member since:
    Feb 2016

    Posted 27 Feb in reply to Link to this post

    Here is a short video of it in actions:

     

    http://screencast.com/t/xjntfMn1rpQ

     

  6.  avatar
    5 posts
    Member since:
    Feb 2016

    Posted 29 Feb Link to this post

    Hello --  

     

    Anybody have some help on this?

    Joe

  7. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 02 Mar Link to this post

    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
Back to Top
UI for WinForms is Visual Studio 2017 Ready