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

Cell tags lost on LoadLayout

2 Answers 115 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Megan
Top achievements
Rank 1
Megan asked on 20 Sep 2019, 03:53 PM

I'm using tags to selectively format cells and using automatic layout saving/loading. When a layout is loaded, cell tags are lost. Example code below. I feel like the cell tag should remain intact and this is a bug? I tried to include a project but only images are allowed on the forum.

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
 
namespace TestGridViewTagsLayout
{
    public class Form1 : Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            Telerik.WinControls.UI.TableViewDefinition tableViewDefinition3 = new Telerik.WinControls.UI.TableViewDefinition();
            this.radGridView1 = new Telerik.WinControls.UI.RadGridView();
            this.cmdLoadLayout = new System.Windows.Forms.Button();
            this.cmdSaveLayout = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1.MasterTemplate)).BeginInit();
            this.SuspendLayout();
            //
            // radGridView1
            //
            this.radGridView1.Location = new System.Drawing.Point(12, 12);
            //
            //
            //
            this.radGridView1.MasterTemplate.ViewDefinition = tableViewDefinition3;
            this.radGridView1.Name = "radGridView1";
            this.radGridView1.Size = new System.Drawing.Size(776, 394);
            this.radGridView1.TabIndex = 0;
            this.radGridView1.CellFormatting += new Telerik.WinControls.UI.CellFormattingEventHandler(this.RadGridView1_CellFormatting);
            //
            // cmdLoadLayout
            //
            this.cmdLoadLayout.Location = new System.Drawing.Point(12, 415);
            this.cmdLoadLayout.Name = "cmdLoadLayout";
            this.cmdLoadLayout.Size = new System.Drawing.Size(75, 23);
            this.cmdLoadLayout.TabIndex = 1;
            this.cmdLoadLayout.Text = "Load Layout";
            this.cmdLoadLayout.UseVisualStyleBackColor = true;
            this.cmdLoadLayout.Click += new System.EventHandler(this.CmdLoadLayout_Click);
            //
            // cmdSaveLayout
            //
            this.cmdSaveLayout.Location = new System.Drawing.Point(93, 415);
            this.cmdSaveLayout.Name = "cmdSaveLayout";
            this.cmdSaveLayout.Size = new System.Drawing.Size(75, 23);
            this.cmdSaveLayout.TabIndex = 1;
            this.cmdSaveLayout.Text = "Save Layout";
            this.cmdSaveLayout.UseVisualStyleBackColor = true;
            this.cmdSaveLayout.Click += new System.EventHandler(this.CmdSaveLayout_Click);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.cmdSaveLayout);
            this.Controls.Add(this.cmdLoadLayout);
            this.Controls.Add(this.radGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1.MasterTemplate)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
            this.ResumeLayout(false);
 
        }
 
        #endregion
 
        private Telerik.WinControls.UI.RadGridView radGridView1;
        private System.Windows.Forms.Button cmdLoadLayout;
        private System.Windows.Forms.Button cmdSaveLayout;
 
        private string layoutXml = null;
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            List<TestEntity> list = new List<TestEntity>();
            list.Add(new TestEntity() { a = "1", b = "1", c = "1" });
            list.Add(new TestEntity() { a = "1", b = "2", c = "2" });
            list.Add(new TestEntity() { a = "3", b = "1", c = "1" });
            list.Add(new TestEntity() { a = "1", b = "1", c = "1" });
 
            radGridView1.AutoGenerateColumns = false;
 
            GridViewTextBoxColumn textBoxColumn = new GridViewTextBoxColumn();
            textBoxColumn.Name = "a";
            textBoxColumn.HeaderText = "A";
            textBoxColumn.FieldName = "a";
            textBoxColumn.AutoSizeMode = BestFitColumnMode.AllCells;
            radGridView1.MasterTemplate.Columns.Add(textBoxColumn);
 
            textBoxColumn = new GridViewTextBoxColumn();
            textBoxColumn.Name = "b";
            textBoxColumn.HeaderText = "B";
            textBoxColumn.FieldName = "b";
            textBoxColumn.AutoSizeMode = BestFitColumnMode.AllCells;
            radGridView1.MasterTemplate.Columns.Add(textBoxColumn);
 
            textBoxColumn = new GridViewTextBoxColumn();
            textBoxColumn.Name = "c";
            textBoxColumn.HeaderText = "C";
            textBoxColumn.FieldName = "c";
            textBoxColumn.AutoSizeMode = BestFitColumnMode.AllCells;
            radGridView1.MasterTemplate.Columns.Add(textBoxColumn);
 
            radGridView1.DataSource = list;
            HighlightFieldChanges();
            radGridView1.TableElement.Update(GridUINotifyAction.Reset);
        }
 
        private void HighlightFieldChanges()
        {
            for (int i = 1; i < radGridView1.RowCount; i++)
            {
                GridViewRowInfo prevRow = radGridView1.Rows[i - 1];
                GridViewRowInfo curRow = radGridView1.Rows[i];
 
                for (int j = 0; j < prevRow.Cells.Count; j++)
                {
                    if (!(Convert.ToString(prevRow.Cells[j].Value).Equals(Convert.ToString(curRow.Cells[j].Value))))
                    {
                        prevRow.Cells[j].Tag = true;
                    }
                }
            }
        }
 
        private void RadGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
        {
            if (((e.Row.Cells[e.ColumnIndex].Tag) as bool?).GetValueOrDefault(false) == true)
            {
                e.CellElement.DrawFill = true;
                e.CellElement.BackColor = Color.LightYellow;
                e.CellElement.NumberOfColors = 1;
            }
            else
            {
                e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
                e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
                e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty, ValueResetFlags.Local);
            }
        }
 
        private void CmdSaveLayout_Click(object sender, EventArgs e)
        {
            MemoryStream ms = new MemoryStream();
            radGridView1.SaveLayout(ms);
            ms.Flush();
            ms.Position = 0;
            StreamReader sr = new StreamReader(ms);
            layoutXml = sr.ReadToEnd();
        }
 
        private void CmdLoadLayout_Click(object sender, EventArgs e)
        {
            MemoryStream ms = new MemoryStream();
            StreamWriter sw = new StreamWriter(ms);
            sw.Write(layoutXml);
            sw.Flush();
            ms.Position = 0;
            radGridView1.LoadLayout(ms);
            radGridView1.TableElement.Update(GridUINotifyAction.Reset);
        }
    }
 
    public class TestEntity
    {
        public string a { get; set; }
        public string b { get; set; }
        public string c { get; set; }
    }
}

2 Answers, 1 is accepted

Sort by
0
Megan
Top achievements
Rank 1
answered on 20 Sep 2019, 03:56 PM
Just in case it isn't clear, please click "Save Layout" and then "Load Layout".
0
Dimitar
Telerik team
answered on 23 Sep 2019, 09:37 AM

Hi Megan,

This is not an issue, we only keep general columns information and the cell are not serialized. In this case, you can set the Tag after the layout is loaded and update the grid with the following method:

private void CmdLoadLayout_Click(object sender, EventArgs e)
{
 
    //other code omited

    HighlightFieldChanges();
    radGridView1.TableElement.Update(GridUINotifyAction.StateChanged);

}

I hope this helps. Should you have any other questions, do not hesitate to ask.

Regards,
Dimitar
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Megan
Top achievements
Rank 1
Answers by
Megan
Top achievements
Rank 1
Dimitar
Telerik team
Share this question
or