Search This Blog

Wednesday, May 20, 2015

windows grid application with multiple cell types




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;

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

        void LoadInfo()
        {
            dataGridView1.ColumnCount = 3;
            dataGridView1.Columns[0].Name = "Person ID";
            dataGridView1.Columns[1].Name = "Person Name";
            dataGridView1.Columns[2].Name = "DOB";

            var x = new PersonRepo().GetPerson();
            foreach (var item in x)
            {
                dataGridView1.Rows.Add(new string[] { item.Id.ToString(),item.Name,
                    item.Dob.ToShortDateString() });
            }

            DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
            cmb.HeaderText = "Select Gender";
            cmb.Name = "cmb";
            cmb.MaxDropDownItems = 4;
            cmb.Items.Add("Male");
            cmb.Items.Add("Female");
            dataGridView1.Columns.Add(cmb);

            foreach (DataGridViewRow item in dataGridView1.Rows) 
            {
                try
                {
                    DataGridViewComboBoxCell a = item.Cells[3] as DataGridViewComboBoxCell;
                    DataGridViewTextBoxCell id = item.Cells[0] as DataGridViewTextBoxCell;
                    a.Value = x.Where(p => p.Id == int.Parse(id.Value.ToString())).FirstOrDefault().Gender;
                }
                catch { break; }
            }
        }

        DateTimePicker oDateTimePicker = new DateTimePicker();
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                dataGridView1.Controls.Add(oDateTimePicker);
                oDateTimePicker.Format = DateTimePickerFormat.Short;
                Rectangle oRectangle = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
                oDateTimePicker.Size = new Size(oRectangle.Width, oRectangle.Height);
                oDateTimePicker.Location = new Point(oRectangle.X, oRectangle.Y); 
                oDateTimePicker.CloseUp += new EventHandler(oDateTimePicker_CloseUp);
                oDateTimePicker.TextChanged += new EventHandler(dateTimePicker_OnTextChange);
                oDateTimePicker.Visible = true;
            }
        }

        private void oDateTimePicker_CloseUp(object sender, EventArgs e)
        {
            oDateTimePicker.Visible = false;
        }
        private void dateTimePicker_OnTextChange(object sender, EventArgs e)
        {
            dataGridView1.CurrentCell.Value = oDateTimePicker.Text.ToString();
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            listBox1.Items.Add($@"row:{e.RowIndex} / col:{e.ColumnIndex} 
                        ==> {dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()}");
        }
    }


    public class PersonRepo {

        Random rd = new Random();
        List<Person> lstPerson = null;
        public PersonRepo()
        {
            lstPerson = new List<Person>();
            for (int i = 0; i < 100; i++)
            {
                lstPerson.Add(new Person {
                    Id = i,
                    Name = $"Name_{i}",
                    Dob = new DateTime(rd.Next(1990, 2010), rd.Next(1, 10), rd.Next(1, 20)),
                    Gender = (rd.Next(0,10)%2==0)?"Male":"Female"
                });
            }

        }

        public List<Person> GetPerson() {

            return lstPerson;
        }

    }

    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime Dob { get; set; }
        public string Gender { get; set; }
    }


}

No comments:

Post a Comment