Completed
Last Updated: 05 Oct 2016 14:00 by ADMIN
ADMIN
Hristo
Created on: 27 Sep 2016 13:21
Category: GridView
Type: Bug Report
3
FIX. RadGridView - when copied the date values should respect the format string property set on the GridViewDateTimeColumn
Workaround: 

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.radGridView1.DataSource = this.GetData();
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;

            ((GridViewDateTimeColumn)this.radGridView1.Columns["Date"]).FormatString = "{0: yyyy-MM-dd hh:mm:ss.fff tt}";
        }

        private DataTable GetData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Date", typeof(DateTime));
            dt.Columns.Add("Bool", typeof(bool));
            for (int i = 0; i < 100; i++)
            {
                dt.Rows.Add(i, "Name " + i, DateTime.Now.AddDays(i), i % 2 == 0);
            }

            return dt;
        }
    }

 public class MyRadGridView : RadGridView
    {
        public override string ThemeClassName
        {
            get
            {
                return typeof(RadGridView).FullName;
            }
        }

        protected override RadGridViewElement CreateGridViewElement()
        {
            return new MyRadGridViewElement();
        }
    }

 public class MyRadGridViewElement : RadGridViewElement
    {
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(MyRadGridViewElement);
            }
        }

        protected override MasterGridViewTemplate CreateTemplate()
        {
            return new MyMasterGridViewTemplate();
        }
    }

 public class MyMasterGridViewTemplate : MasterGridViewTemplate
    {
        public override void Copy()
        {
            base.Copy();

            GridViewCellInfo[] cells = null;
            if (this.SelectionMode == GridViewSelectionMode.CellSelect)
            {
                cells = new GridViewCellInfo[this.SelectedCells.Count];
                this.SelectedCells.CopyTo(cells, 0);
            }
            else if (this.SelectionMode == GridViewSelectionMode.FullRowSelect)
            {
                GridViewDataRowInfo row = this.SelectedRows[0] as GridViewDataRowInfo;
                if (this.SelectedRows.Count == 1 && row.ViewTemplate.CurrentColumn != null)
                {
                    cells = new GridViewCellInfo[row.Cells.Count];
                    for (int i = 0; i < row.Cells.Count; i++)
                    {
                        cells[i] = row.Cells[i];
                    }
                }
            }

            if (Clipboard.GetData(DataFormats.Text) != null)
            {
                string data = Clipboard.GetData(DataFormats.Text).ToString();
                if (data != string.Empty && cells != null)
                {
                    var values = data.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);

                    StringBuilder sb = new StringBuilder();
                    foreach (string value in values)
                    {
                        DateTime date;
                        if (DateTime.TryParse(value, out date))
                        {
                            string baseFormat = "yyyy-MM-dd HH:mm tt";
                            foreach (var cell in cells)
                            {
                                if (cell.ColumnInfo is GridViewDateTimeColumn && ((DateTime)cell.Value).ToString(baseFormat) == date.ToString(baseFormat))
                                {
                                    sb.Append(string.Format(((GridViewDateTimeColumn)cell.ColumnInfo).FormatString, cell.Value) + "\t");
                                    break;
                                }
                            }
                        }
                        else
                        {
                            sb.Append(value + "\t");
                        }
                    }

                    Clipboard.Clear();
                    Clipboard.SetData(DataFormats.Text, sb.ToString());
                }
            }
        }
    }
0 comments