using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;

public partial class OutlookDemo : System.Web.UI.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)
    {
        String ConnString = ConfigurationManager.ConnectionStrings["TelerikConnectionString"].ConnectionString;
        SqlConnection MySqlConnection = new SqlConnection(ConnString);

        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
            {
                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;
        }
    }
}
