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

namespace DataEntry
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
        {
            if (this.radGridView1.CurrentColumn.Name == "Value" && this.radGridView1.CurrentRow.Cells[2].Value.ToString() == "DropDown")
            {
                RadDropDownListEditor editor = new RadDropDownListEditor();
                RadDropDownListEditorElement element = editor.EditorElement as RadDropDownListEditorElement;
                element.DropDownStyle = RadDropDownStyle.DropDownList;
                element.AutoSizeItems = true;
                var selection = this.radGridView1.CurrentRow.Cells[3].Value.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
                element.DataSource = selection;
                //element.DisplayMember = "Name";
                //element.ValueMember = "Name";

                e.Editor = editor;
            }
            else if (this.radGridView1.CurrentColumn.Name == "Value" && this.radGridView1.CurrentRow.Cells[2].Value.ToString() == "Date")
            {
                RadDateTimeEditor editor = new RadDateTimeEditor();
                editor.CustomFormat = "dd-MMM-yyyy";
                e.Editor = editor;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //RadGridView
            radGridView1.Columns.Add("Item");
            radGridView1.Columns.Add("Value");
            radGridView1.Columns.Add("Type");
            radGridView1.Columns.Add("Options");

            this.radGridView1.EditorRequired += radGridView1_EditorRequired;

            List<string[]> list= new List<string[]>();
            list.Add(new string[] { "Result", "DropDown", "Pass,Failed" });
            list.Add(new string[] { "UserId", "Text", "" });
            list.Add(new string[] { "Number", "Number", "" });
            list.Add(new string[] { "Date", "Date", "" });
            int index = 0;
            foreach(string[] s in list)
            {
                radGridView1.Rows.Add(s[0], "", s[1], s[2]);
                switch(s[1])
                {
                    case "DropDown":
                        var selection = s[2].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
                        break;
                }
                index++;
            }


            

        }


        private void radGridView1_CellValidating(object sender, CellValidatingEventArgs e)
        {
            //GridViewDataColumn column = e.Column as GridViewDataColumn;
            //if (e.Row is GridViewDataRowInfo && column != null && column.Name == "Value")
            //{
            //    if (string.IsNullOrEmpty((string)e.Value) || ((string)e.Value).Trim() == string.Empty)
            //    {
            //        e.Cancel = true;
            //        ((GridViewDataRowInfo)e.Row).ErrorText = "Validation error!";
            //    }
            //    else
            //    {
            //        if (e.Row.Cells["Type"].Value.ToString() == "Number")
            //        {
            //            var isNumeric = int.TryParse(e.Value.ToString(), out _);
            //            if(!isNumeric)
            //            {
            //                MessageBox.Show("Numbers only!");
            //                e.Cancel = true;
            //                ((GridViewDataRowInfo)e.Row).ErrorText = "Numeric Value only!";
            //            }
            //        }
            //        ((GridViewDataRowInfo)e.Row).ErrorText = string.Empty;
            //    }
            //}
        }
    }
}
