using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.Data;
using Telerik.WinControls.UI;

namespace _133282GidFilterSearchAfterRefresh
{
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();

            List<MessageSort> messages = new List<MessageSort>();
            for (int i = 0; i < 100; i++)
            {
                if (i % 2 == 0)
                {
                    messages.Add(new MessageSort(i,"FILE",DateTime.Now.AddMinutes(-i),i % 2 == 0,i % 2 == 0,i,i / 2,Guid.NewGuid().ToString()));
                }
                else
                {
                    messages.Add(new MessageSort(i,"MAIL",DateTime.Now.AddMinutes(-i),i % 2 == 0,i % 2 == 0,i,i / 2,Guid.NewGuid().ToString()));
                }
            }
            this.radGridView1.DataSource = messages;
            this.radGridView1.BestFitColumns();
            this.radGridView1.AllowSearchRow = true;
            this.radGridView1.EnableFiltering = true;

            // this.timer1.Start();

            this.radGridView1.FilterChanged += radGridView1_FilterChanged;

            FilterDescriptor filter = new FilterDescriptor();
            filter.PropertyName = "MessageSource";
            filter.Operator = FilterOperator.StartsWith;
            filter.Value = "FILE";
            filter.IsFilterEditor = true;
            this.radGridView1.FilterDescriptors.Add(filter);
        }

        private void radGridView1_FilterChanged(object sender, GridViewCollectionChangedEventArgs e)
        {
            Console.WriteLine(this.radGridView1.FilterDescriptors.Expression);
        }

        private void RadForm1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'nwindDataSet.Products' table. You can move, or remove it, as needed.
            this.productsTableAdapter.Fill(this.nwindDataSet.Products);
            this.radGridView1.BestFitColumns();
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            this.radGridView1.MasterTemplate.Refresh();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //this.radGridView1.MasterTemplate.Refresh();
            //Application.DoEvents();
            //this.radGridView1.MasterView.TableFilteringRow.Cells[this.radGridView1.CurrentColumn.Name].BeginEdit();
            this.radGridView1.BeginUpdate();
            for (int i = 0; i < 10; i++)
            {
                this.radGridView1.Rows.Add(Guid.NewGuid().ToString());
            }
            this.radGridView1.EndUpdate();
            if (radGridView1.Rows.Count > 0)
                radGridView1.TableElement.ScrollToRow(radGridView1.Rows.Last());

            Application.DoEvents(); 
            this.radGridView1.MasterView.TableFilteringRow.Cells[0].BeginEdit();
        }

        public class MessageSort
        {
            public int MessageId { get; set; }

            public string MessageSource { get; set; }

            public DateTime? ReceiveDate { get; set; }

            public bool PriorityFlag { get; set; }

            public bool? Tracked { get; set; }

            public int TotalPages { get; set; }

            public int? ProcessedPages { get; set; }

            public string ProcessBy { get; set; }

            public MessageSort(int messageId, string messageSource, DateTime? receiveDate, bool priorityFlag,
                bool? tracked, int totalPages, int? processedPages, string processBy)
            {
                this.MessageId = messageId;
                this.MessageSource = messageSource;
                this.ReceiveDate = receiveDate;
                this.PriorityFlag = priorityFlag;
                this.Tracked = tracked;
                this.TotalPages = totalPages;
                this.ProcessedPages = processedPages;
                this.ProcessBy = processBy;
            }
        }
    }
}