using System;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class FrmGridViewNoRespone : Form
    {
        DataTable table = null;
        public FrmGridViewNoRespone()
        {
            InitializeComponent();
            table = new DataTable();

            this.radGridView1.AllowAddNewRow = false;
            this.radGridView1.AllowColumnReorder = false;
            this.radGridView1.AllowDeleteRow = false;
            this.radGridView1.AllowDragToGroup = true;
            this.radGridView1.AllowEditRow = false;
            this.radGridView1.EnableGrouping = true;
            this.radGridView1.AllowRowResize = false;
            this.radGridView1.AutoGenerateColumns = false;
            this.radGridView1.ShowGroupPanel = true;
            this.radGridView1.ShowRowHeaderColumn = false;
            this.radGridView1.EnableCustomFiltering = true;

            this.radGridView1.ShowFilteringRow = false;
            this.radGridView1.EnableHotTracking = false;
            this.radGridView1.EnableCustomSorting = true;
            this.radGridView1.AutoExpandGroups = true;



            this.radGridView1.Columns.Add("ID", "ID", "ID");
            this.radGridView1.Columns.Add("ParentID", "ParentID", "ParentID");
            this.radGridView1.Columns.Add("Name", "Name", "Name");
            this.radGridView1.Columns.Add("DateTime", "DateTime", "DateTime");
            this.radGridView1.Columns.Add("Group", "Group", "Group");

            this.radGridView1.Columns["ID"].Width = 80;
            this.radGridView1.Columns["ParentID"].Width = 100;
            this.radGridView1.Columns["Name"].Width = 150;
            this.radGridView1.Columns["DateTime"].Width = 150;
            this.radGridView1.Columns["Group"].Width = 150;



            table.Clear();
            table = GetTableSchema();

            this.radGridView1.DataSource = table;

            this.radGridView1.Relations.Clear();

            this.radGridView1.GroupDescriptors.Expression = "Group";  //group by ...

            this.radGridView1.Relations.AddSelfReference(this.radGridView1.MasterTemplate, "ID", "ParentID");
            //
        }

        private DataTable GetTableSchema()
        {
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("ParentID", typeof(int));
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("DateTime", typeof(DateTime));
            table.Columns.Add("Group", typeof(string));
            return table;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {

            Stopwatch sw = new Stopwatch();
            sw.Reset();
            sw.Start();

            table = GetTableSchema();
            table.BeginLoadData();

            DataRow row = table.NewRow();
            row[0] = 9;
            row[1] = 0;
            row[2] = "Visual Basic";
            row[3] = DateTime.Now;
            row[4] = "Basic";
            table.Rows.Add(row);

            row = table.NewRow();
            row[0] = 10;
            row[1] = 9;
            row[2] = "VB6";
            row[3] = DateTime.Now;
            row[4] = "Basic";
            table.Rows.Add(row);

            table.Rows.Add(1, 0, "C/C++", DateTime.Now, "C");
            table.Rows.Add(2, 1, "VC++", DateTime.Now, "C");
            table.Rows.Add(3, 1, "BC++", DateTime.Now, "C");
            table.Rows.Add(4, 1, "TC", DateTime.Now, "C");

            table.Rows.Add(5, 0, "Web", DateTime.Now, "web");
            table.Rows.Add(6, 5, "JS", DateTime.Now, "web");
            table.Rows.Add(7, 5, "CSS", DateTime.Now, "web");
            table.Rows.Add(8, 5, "HTML", DateTime.Now, "web");

            for (int i = 9; i < 10000; i += 4)
            {
                string index = (i % 9).ToString();

                table.Rows.Add(i, 0, "Web" + index, DateTime.Now, "web" + index);
                table.Rows.Add(i + 1, i, "JS", DateTime.Now, "web" + index);
                table.Rows.Add(i + 2, i, "CSS", DateTime.Now, "web" + index);
                table.Rows.Add(i + 3, i, "HTML", DateTime.Now, "web" + index);
            }

            table.EndLoadData();

            Console.WriteLine("NO RESPONSE?  -->>>");

            this.radGridView1.Invoke(new Action(delegate()
                {
                    this.radGridView1.TableElement.BeginUpdate();
                    this.radGridView1.TableElement.ViewElement.ClearRows();
                    this.radGridView1.DataSource = table;
                    this.radGridView1.TableElement.EndUpdate();
                }));

            Console.WriteLine("NO RESPONSE?  <<< ---");

            this.Invoke(new Action(delegate()
            {
                this.Text = sw.Elapsed.ToString();
            }));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.timer2.Enabled = !this.timer2.Enabled;
        }
    }
}
