using System;
using System.Data;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI;

namespace Telerik.GridExamplesCSharp.Programming.WebMail
{
    public partial class DefaultCS : Page
    {
        private DataTable Mails
        {
            get
            {
                object res = this.ViewState["Mails"];
                if (res != null)
                {
                    return (DataTable)res;
                }

                DataTable table = this.GetDataTable("SELECT * FROM Mails");
                this.ViewState["Mails"] = table;
                return table;
            }
        }

        private void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            RadGrid1.DataSource = this.Mails;
        }

        protected void ToggleRowSelection(object sender, EventArgs e)
        {
            ((sender as CheckBox).Parent.Parent as GridItem).Selected = (sender as CheckBox).Checked;
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        ///		Required method for Designer support - do not modify
        ///		the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.RadGrid1.NeedDataSource += new Telerik.Web.UI.GridNeedDataSourceEventHandler(this.RadGrid1_NeedDataSource);
            this.RadGrid1.ItemCommand += new GridCommandEventHandler(RadGrid1_ItemCommand);
            this.RadGrid1.ItemCreated += new GridItemEventHandler(RadGrid1_ItemCreated);
        }
        #endregion

        private DataTable GetDataTable(String queryString)
        {
            //SqlConnection MySqlConnection = new SqlConnection(@"Data Source=S81373;Max Pool Size=200;initial catalog=PFTelerik;Integrated Security=SSPI;");
            SqlConnection MySqlConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Telerik.mdf;Integrated Security=True;User Instance=True");
            SqlDataAdapter MySqlDataAdapter = new SqlDataAdapter();
            MySqlDataAdapter.SelectCommand = new SqlCommand(queryString, MySqlConnection);

            DataTable myDataTable = new DataTable();
            MySqlConnection.Open();
            try
            {
                MySqlDataAdapter.Fill(myDataTable);
            }
            finally
            {
                MySqlConnection.Close();
            }

            return myDataTable;
        }

        private void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "ChangeFlag")
            {
                DataRow[] changedRows = this.Mails.Select("MailID = " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["MailID"]);
                if (changedRows.Length != 1)
                {
                    e.Canceled = true;
                    return;
                }

                changedRows[0].BeginEdit();
                try
                {
                    changedRows[0]["Flagged"] = Convert.ToBoolean(changedRows[0]["Flagged"]) ? false : true;
                    changedRows[0].EndEdit();
                }
                catch (Exception ex)
                {
                    changedRows[0].CancelEdit();
                    e.Canceled = true;
                }
                e.Item.OwnerTableView.Rebind();
            }
        }

        private void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                e.Item.ForeColor = (Convert.ToBoolean(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Flagged"])) ? System.Drawing.Color.Red : System.Drawing.Color.Empty;
            }
        }
    }
}
