Thursday, July 16, 2020

DataGrid CellFormatting

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 DataGrid
{
    public partial class Form1 : Form
    {
        private Panel buttonPanel = new Panel();
        private DataGridView customerDataGridView = new DataGridView();
        private Button addNewRowButton = new Button();
        private Button deleteRowButton = new Button();

        public Form1()
        {
            this.Load += new EventHandler(Form1_Load);
        }

        private void Form1_Load(System.Object sender, System.EventArgs e)
        {
            SetupLayout();
            SetupDataGridView();
            PopulateDataGridView();
        }

        private void songsDataGridView_CellFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
        {
            if(e != null)
            {
                if (this.customerDataGridView.Columns[e.ColumnIndex].Name == "FirstName")
                {
                    if (e.Value != null)
                    {
                        // Check for the string "pink" in the cell.
                        string stringValue = (string)e.Value;
                        stringValue = stringValue.ToLower();
                        if ((stringValue.IndexOf("ram") > -1))
                        {
                            e.CellStyle.BackColor = Color.Pink;
                        }
                    }
                }
                if (this.customerDataGridView.Columns[e.ColumnIndex].Name == "DOB")
                {
                    if (e.Value != null)
                    {
                        try
                        {
                            e.Value = DateTime.Parse(e.Value.ToString()).ToLongDateString();
                            e.FormattingApplied = true;
                        }
                        catch(FormatException)
                        {
                            Console.WriteLine("{0} is not a valid date", e.Value.ToString());
                        }
                    }
                }
            }
        }

        private void addNewRowButton_Click(object sender, EventArgs e)
        {
            this.customerDataGridView.Rows.Add();
        }

        private void deleteRowButton_Click(object sender, EventArgs e)
        {
            if (this.customerDataGridView.SelectedRows.Count > 0 &&
                this.customerDataGridView.SelectedRows[0].Index !=
                this.customerDataGridView.Rows.Count - 1)
            {
                this.customerDataGridView.Rows.RemoveAt(
                    this.customerDataGridView.SelectedRows[0].Index);
            }
        }

        private void SetupLayout()
        {
            this.Size = new Size(600, 500);

            addNewRowButton.Text = "Add Row";
            addNewRowButton.Location = new Point(10, 10);
            addNewRowButton.Click += new EventHandler(addNewRowButton_Click);

            deleteRowButton.Text = "Delete Row";
            deleteRowButton.Location = new Point(100, 10);
            deleteRowButton.Click += new EventHandler(deleteRowButton_Click);

            buttonPanel.Controls.Add(addNewRowButton);
            buttonPanel.Controls.Add(deleteRowButton);
            buttonPanel.Height = 50;
            buttonPanel.Dock = DockStyle.Bottom;

            this.Controls.Add(this.buttonPanel);
        }

        private void SetupDataGridView()
        {
            this.Controls.Add(customerDataGridView);

            customerDataGridView.ColumnCount = 4;

            customerDataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;
            customerDataGridView.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
            customerDataGridView.ColumnHeadersDefaultCellStyle.Font =
                new Font(customerDataGridView.Font, FontStyle.Bold);

            customerDataGridView.Name = "customerDataGridView";
            customerDataGridView.Location = new Point(8, 8);
            customerDataGridView.Size = new Size(500, 250);
            customerDataGridView.AutoSizeRowsMode =
                DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
            customerDataGridView.ColumnHeadersBorderStyle =
                DataGridViewHeaderBorderStyle.Single;
            customerDataGridView.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            customerDataGridView.GridColor = Color.Black;
            customerDataGridView.RowHeadersVisible = false;

            customerDataGridView.Columns[0].Name = "PersonID";
            customerDataGridView.Columns[1].Name = "FirstName";
            customerDataGridView.Columns[2].Name = "Country";
            customerDataGridView.Columns[3].Name = "DOB";
            customerDataGridView.Columns[0].DefaultCellStyle.Font =
                new Font(customerDataGridView.DefaultCellStyle.Font, FontStyle.Italic);

            customerDataGridView.SelectionMode =
                DataGridViewSelectionMode.FullRowSelect;
            customerDataGridView.MultiSelect = false;
            customerDataGridView.Dock = DockStyle.Fill;

            customerDataGridView.CellFormatting += new
                DataGridViewCellFormattingEventHandler(
                songsDataGridView_CellFormatting);
        }

        private void PopulateDataGridView()
        {

            string[] row0 = {"1","Ram","India","1/10/1970"};
            string[] row1 = {"2","Rahul","India", "2/23/1972" };
            string[] row2 = {"3","Soni","India", "3/24/1986" };
            string[] row3 = {"4","Vicky","India", "4/25/1980" };
            string[] row4 = {"5","Lynda","UK","5/26/1990"};
            string[] row5 = {"6","Kim","US","6/27/1986"};
            string[] row6 = {"7","Miller","US","7/28/1990"};

            customerDataGridView.Rows.Add(row0);
            customerDataGridView.Rows.Add(row1);
            customerDataGridView.Rows.Add(row2);
            customerDataGridView.Rows.Add(row3);
            customerDataGridView.Rows.Add(row4);
            customerDataGridView.Rows.Add(row5);
            customerDataGridView.Rows.Add(row6);

            customerDataGridView.Columns[0].DisplayIndex = 0;
            customerDataGridView.Columns[1].DisplayIndex = 1;
            customerDataGridView.Columns[2].DisplayIndex = 3;
            customerDataGridView.Columns[3].DisplayIndex = 2;
         
        }
    }
}