namespace Telerik.Reporting.Examples.CSharp.WinDemo
{
    using System;
    using System.ComponentModel;
    using System.Windows.Forms;

    public partial class ReportSelector : Form
	{
		public ReportSelector()
		{
			InitializeComponent();
			
			this.PopulateGrid();
			this.dataGridView1.CellContentClick += new DataGridViewCellEventHandler(this.OnCellContentClick);
		}

		void OnCellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			if (0 == e.ColumnIndex && e.RowIndex >= 0)
			{
				DataGridViewCell cell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
				Type t = cell.Tag as System.Type;
				if (null != t)
				{
                    IReportDocument reportDocument = Activator.CreateInstance(t) as IReportDocument;
					if (null != reportDocument)
					{
						MainForm frm = new MainForm(reportDocument);
						frm.WindowState = FormWindowState.Normal;
						frm.Show(this);
					}
				}
			}
		}

		void PopulateGrid()
		{
			this.dataGridView1.Rows.Clear();
			
			foreach (Type t in ReportExplorer.GetReports())
			{
				DescriptionAttribute description = TypeDescriptor.GetAttributes(t)[typeof(DescriptionAttribute)] as DescriptionAttribute;
				
				object[] values = new object[]
					{
						t.Name
						, (!description.IsDefaultAttribute() ? description.Description : "-")
					};
				
				int rowIndex = this.dataGridView1.Rows.Add(values);
				this.dataGridView1.Rows[rowIndex].Cells[0].Tag = t;
			}
		}
	}
}