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 DataGridViewErrors
{
    public partial class frm14DataGridViewErrors : Form
    {
        public frm14DataGridViewErrors()
        {
            InitializeComponent();
        }

        private void frm14DataGridViewErrors_Load(object sender, EventArgs e)
        {
            // Make the DataTable object.
            DataTable dt = new DataTable("People");

            // Add columns to the DataTable.
            dt.Columns.Add("First Name", System.Type.GetType("System.String"));
            dt.Columns.Add("Last Name", System.Type.GetType("System.String"));
            dt.Columns.Add("Occupation", System.Type.GetType("System.String"));
            dt.Columns.Add("Salary", System.Type.GetType("System.Int32"));

            // Make all columns required.
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                dt.Columns[i].AllowDBNull = false;
            }

            // Make First Name + Last Name require uniqueness.
            DataColumn[] unique_cols =
            {
                dt.Columns["First Name"],
                dt.Columns["Last Name"]
            };
            dt.Constraints.Add(new UniqueConstraint(unique_cols));

            // Add items to the table.
            dt.Rows.Add(new object[] { "Rod", "Stephens", "Nerd", 10000 });
            dt.Rows.Add(new object[] { "Sergio", "Aragones", "Cartoonist", 20000 });
            dt.Rows.Add(new object[] { "Eoin", "Colfer", "Author", 30000 });
            dt.Rows.Add(new object[] { "Terry", "Pratchett", "Author", 40000 });

            // Prohibited: First Name is missing.
            //dt.Rows.Add(new object[] { null, "Aragones", "Cartoonist", 20000 });

            // Prohibited: Duplicate First Name + Last Name.
            //dt.Rows.Add(new object[] { "Eoin", "Colfer", "President", 200000 });

            // Make the DataGridView use the DataTable as its data source.
            peopleDataGridView.DataSource = dt;
        }

        // Display information about the error.
        private void peopleDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            // Display an error message.
            string text = "Error updating the record" +
                "\n\n" + e.Exception.Message;
            MessageBox.Show(text, "Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
        }


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.peopleDataGridView = new System.Windows.Forms.DataGridView();
            ((System.ComponentModel.ISupportInitialize)(this.peopleDataGridView)).BeginInit();
            this.SuspendLayout();
            // 
            // peopleDataGridView
            // 
            this.peopleDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.peopleDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
            this.peopleDataGridView.Location = new System.Drawing.Point(0, 0);
            this.peopleDataGridView.Name = "peopleDataGridView";
            this.peopleDataGridView.Size = new System.Drawing.Size(456, 157);
            this.peopleDataGridView.TabIndex = 2;
            this.peopleDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.peopleDataGridView_DataError);
            // 
            // frm14DataGridViewErrors
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(456, 157);
            this.Controls.Add(this.peopleDataGridView);
            this.Name = "frm14DataGridViewErrors";
            this.Text = "DataGridViewErrors";
            this.Load += new System.EventHandler(this.frm14DataGridViewErrors_Load);
            ((System.ComponentModel.ISupportInitialize)(this.peopleDataGridView)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.DataGridView peopleDataGridView;
    }
}