using System;
using System.Data;
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 System.Collections.Generic;
using System.Data.OleDb;
using System.Data.SqlClient;
using Telerik.WebControls;
using Telerik.RadToolbarUtils;

namespace TableExplorer
{
    public partial class _Default : System.Web.UI.Page
    {

        const string ConnectionString =
            @"Data Source=localhost\Telerik;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTableCombo();
            }
        }

        private RadComboBox rcbTables
        {
            get
            {
                return (RadComboBox)rttbTables.FindControl("rcbTables");
            }
        }

        private TextBox tbFilter
        {
            get
            {
                return (TextBox)rttbFilter.FindControl("tbFilter");
            }
        }

        private RadComboBox rcbFilter
        {
            get
            {
                return (RadComboBox)rttbFilter.FindControl("rcbFilter");
            }
        }

        private DataTable SelectedTable
        {
            get
            {
                return GetSelectedTable(rcbTables.Text);
            }
        }

        private bool Filtered
        {
            get
            {
                return ((RadToolbarToggleButton)RadToolbar1.Items.GetButtonByCommandName("Filter")).Toggled;
            }
            set
            {
                ((RadToolbarToggleButton)RadToolbar1.Items.GetButtonByCommandName("Filter")).Toggled = value;
            }
        }

        private DataTable GetSelectedTable(string table)
        {
            string sql = "SELECT * FROM [" + table + "]";
            SqlConnection connection = new SqlConnection(ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            return dataSet.Tables[0];
        }

        private void BindTableCombo()
        {
            SqlConnection connection = new SqlConnection(ConnectionString);
            connection.Open();

            string[] restrictions = new string[4];
            restrictions[0] = null; // catalogs
            restrictions[1] = null; // all owners
            restrictions[2] = null; // all tables
            restrictions[3] = "BASE TABLE"; // User created tables only, no views or system
            DataTable schemaTable = connection.GetSchema("Tables", restrictions);

            rcbTables.DataSource = schemaTable;
            rcbTables.DataTextField = "TABLE_NAME";
            rcbTables.DataBind();
        }

        private void BindGrid()
        {
            string sqlFormat = "{0} like '{1}%'";

            DataView currentView = SelectedTable.DefaultView;

            if ((tbFilter.Text.Length > 0) && this.Filtered)
            {
                string sql = string.Format(sqlFormat, rcbFilter.Text, tbFilter.Text);
                currentView.RowFilter = sql;
            }
            RadGrid1.DataSource = currentView;
            RadGrid1.DataBind();
        }

        private void BindColumnCombo()
        {
            List<string> TextColumns = new List<string>();
            foreach (DataColumn column in SelectedTable.Columns)
            {
                if (column.DataType == typeof(System.String))
                {
                    TextColumns.Add(column.Caption);
                }
            }

            rcbFilter.DataSource = TextColumns;
            rcbFilter.DataBind();
        }

        protected void RadToolbar1_OnClick(object sender, Telerik.WebControls.RadToolbarClickEventArgs e)
        {
            switch (e.Button.CommandName)
            {
                case "Open":
                    {
                        this.Filtered = false;
                        tbFilter.Text = "";
                        BindGrid();
                        BindColumnCombo();
                        break;
                    }
                case "Filter":
                    {
                        BindGrid();
                        break;
                    }
            }
        }
    }
}
