DataGrid View: on data error : DataGrid « ADO.Net « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


public class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        List<SimpleDataItem> items = new List<SimpleDataItem>();
        for (int i = 0; i < 10; i++)
        {
            SimpleDataItem item = new SimpleDataItem();
            item.SomeVal = i;
            item.Var = "Hi";
            items.Add(item);

        }


        simpleDataItemBindingSource.DataSource = items;
    }

    private void OnDataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        System.Console.WriteLine(e.Exception.GetType());
        System.Console.WriteLine(e.Exception.Message);
        System.Console.WriteLine(e.Context);

    }
    private void InitializeComponent()
    {

        this.simpleDataItemDataGridView = new System.Windows.Forms.DataGridView();
        this.SomeVal = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.Var = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.simpleDataItemBindingNavigator = new System.Windows.Forms.BindingNavigator();
        this.bindingNavigatorAddNewItem = new System.Windows.Forms.ToolStripButton();
        this.bindingNavigatorCountItem = new System.Windows.Forms.ToolStripLabel();
        this.bindingNavigatorDeleteItem = new System.Windows.Forms.ToolStripButton();
        this.bindingNavigatorMoveFirstItem = new System.Windows.Forms.ToolStripButton();
        this.bindingNavigatorMovePreviousItem = new System.Windows.Forms.ToolStripButton();
        this.bindingNavigatorSeparator = new System.Windows.Forms.ToolStripSeparator();
        this.bindingNavigatorPositionItem = new System.Windows.Forms.ToolStripTextBox();
        this.bindingNavigatorSeparator1 = new System.Windows.Forms.ToolStripSeparator();
        this.bindingNavigatorMoveNextItem = new System.Windows.Forms.ToolStripButton();
        this.bindingNavigatorMoveLastItem = new System.Windows.Forms.ToolStripButton();
        this.bindingNavigatorSeparator2 = new System.Windows.Forms.ToolStripSeparator();
        this.bindingNavigatorSaveItem = new System.Windows.Forms.ToolStripButton();
        this.dataGridViewTextBoxColumn3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.dataGridViewTextBoxColumn4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.simpleDataItemBindingSource = new System.Windows.Forms.BindingSource();
        ((System.ComponentModel.ISupportInitialize)(this.simpleDataItemDataGridView)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.simpleDataItemBindingNavigator)).BeginInit();
        this.simpleDataItemBindingNavigator.SuspendLayout();
        ((System.ComponentModel.ISupportInitialize)(this.simpleDataItemBindingSource)).BeginInit();
        this.SuspendLayout();
        // 
        // simpleDataItemDataGridView
        // 
        this.simpleDataItemDataGridView.AutoGenerateColumns = false;
        this.simpleDataItemDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.SomeVal,
            this.Var});
        this.simpleDataItemDataGridView.Location = new System.Drawing.Point(27, 32);
        this.simpleDataItemDataGridView.Name = "simpleDataItemDataGridView";
        this.simpleDataItemDataGridView.Size = new System.Drawing.Size(300, 220);
        this.simpleDataItemDataGridView.TabIndex = 1;
        // 
        // SomeVal
        // 
        this.SomeVal.DataPropertyName = "SomeVal";
        this.SomeVal.HeaderText = "SomeVal";
        this.SomeVal.Name = "SomeVal";
        // 
        // Var
        // 
        this.Var.DataPropertyName = "Var";
        this.Var.HeaderText = "Var";
        this.Var.Name = "Var";
        // 
        // dataGridView1
        // 
        this.dataGridView1.AutoGenerateColumns = false;
        this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.dataGridViewTextBoxColumn3,
            this.dataGridViewTextBoxColumn4});
        this.dataGridView1.DataSource = this.simpleDataItemBindingSource;
        this.dataGridView1.Location = new System.Drawing.Point(27, 39);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(300, 220);
        this.dataGridView1.TabIndex = 3;
        this.dataGridView1.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnDataError);
        // 
        // simpleDataItemBindingNavigator
        // 
        this.simpleDataItemBindingNavigator.AddNewItem = this.bindingNavigatorAddNewItem;
        this.simpleDataItemBindingNavigator.BindingSource = this.simpleDataItemBindingSource;
        this.simpleDataItemBindingNavigator.CountItem = this.bindingNavigatorCountItem;
        this.simpleDataItemBindingNavigator.DeleteItem = this.bindingNavigatorDeleteItem;
        this.simpleDataItemBindingNavigator.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.bindingNavigatorMoveFirstItem,
            this.bindingNavigatorMovePreviousItem,
            this.bindingNavigatorSeparator,
            this.bindingNavigatorPositionItem,
            this.bindingNavigatorCountItem,
            this.bindingNavigatorSeparator1,
            this.bindingNavigatorMoveNextItem,
            this.bindingNavigatorMoveLastItem,
            this.bindingNavigatorSeparator2,
            this.bindingNavigatorAddNewItem,
            this.bindingNavigatorDeleteItem,
            this.bindingNavigatorSaveItem});
        this.simpleDataItemBindingNavigator.Location = new System.Drawing.Point(0, 0);
        this.simpleDataItemBindingNavigator.MoveFirstItem = this.bindingNavigatorMoveFirstItem;
        this.simpleDataItemBindingNavigator.MoveLastItem = this.bindingNavigatorMoveLastItem;
        this.simpleDataItemBindingNavigator.MoveNextItem = this.bindingNavigatorMoveNextItem;
        this.simpleDataItemBindingNavigator.MovePreviousItem = this.bindingNavigatorMovePreviousItem;
        this.simpleDataItemBindingNavigator.Name = "simpleDataItemBindingNavigator";
        this.simpleDataItemBindingNavigator.PositionItem = this.bindingNavigatorPositionItem;
        this.simpleDataItemBindingNavigator.Size = new System.Drawing.Size(375, 25);
        this.simpleDataItemBindingNavigator.TabIndex = 2;
        this.simpleDataItemBindingNavigator.Text = "bindingNavigator1";
        // 
        // bindingNavigatorAddNewItem
        // 
        this.bindingNavigatorAddNewItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
        this.bindingNavigatorAddNewItem.Name = "bindingNavigatorAddNewItem";
        this.bindingNavigatorAddNewItem.Size = new System.Drawing.Size(23, 22);
        this.bindingNavigatorAddNewItem.Text = "Add new";
        // 
        // bindingNavigatorCountItem
        // 
        this.bindingNavigatorCountItem.Name = "bindingNavigatorCountItem";
        this.bindingNavigatorCountItem.Size = new System.Drawing.Size(36, 22);
        this.bindingNavigatorCountItem.Text = "of {0}";
        this.bindingNavigatorCountItem.ToolTipText = "Total number of items";
        // 
        // bindingNavigatorDeleteItem
        // 
        this.bindingNavigatorDeleteItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
        this.bindingNavigatorDeleteItem.Name = "bindingNavigatorDeleteItem";
        this.bindingNavigatorDeleteItem.Size = new System.Drawing.Size(23, 22);
        this.bindingNavigatorDeleteItem.Text = "Delete";
        // 
        // bindingNavigatorMoveFirstItem
        // 
        this.bindingNavigatorMoveFirstItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
        this.bindingNavigatorMoveFirstItem.Name = "bindingNavigatorMoveFirstItem";
        this.bindingNavigatorMoveFirstItem.Size = new System.Drawing.Size(23, 22);
        this.bindingNavigatorMoveFirstItem.Text = "Move first";
        // 
        // bindingNavigatorMovePreviousItem
        // 
        this.bindingNavigatorMovePreviousItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
        this.bindingNavigatorMovePreviousItem.Name = "bindingNavigatorMovePreviousItem";
        this.bindingNavigatorMovePreviousItem.Size = new System.Drawing.Size(23, 22);
        this.bindingNavigatorMovePreviousItem.Text = "Move previous";
        // 
        // bindingNavigatorSeparator
        // 
        this.bindingNavigatorSeparator.Name = "bindingNavigatorSeparator";
        this.bindingNavigatorSeparator.Size = new System.Drawing.Size(6, 25);
        // 
        // bindingNavigatorPositionItem
        // 
        this.bindingNavigatorPositionItem.Name = "bindingNavigatorPositionItem";
        this.bindingNavigatorPositionItem.Size = new System.Drawing.Size(50, 25);
        this.bindingNavigatorPositionItem.Text = "0";
        this.bindingNavigatorPositionItem.ToolTipText = "Current position";
        // 
        // bindingNavigatorSeparator1
        // 
        this.bindingNavigatorSeparator1.Name = "bindingNavigatorSeparator1";
        this.bindingNavigatorSeparator1.Size = new System.Drawing.Size(6, 25);
        // 
        // bindingNavigatorMoveNextItem
        // 
        this.bindingNavigatorMoveNextItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
        this.bindingNavigatorMoveNextItem.Name = "bindingNavigatorMoveNextItem";
        this.bindingNavigatorMoveNextItem.Size = new System.Drawing.Size(23, 22);
        this.bindingNavigatorMoveNextItem.Text = "Move next";
        // 
        // bindingNavigatorMoveLastItem
        // 
        this.bindingNavigatorMoveLastItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
        this.bindingNavigatorMoveLastItem.Name = "bindingNavigatorMoveLastItem";
        this.bindingNavigatorMoveLastItem.Size = new System.Drawing.Size(23, 22);
        this.bindingNavigatorMoveLastItem.Text = "Move last";
        // 
        // bindingNavigatorSeparator2
        // 
        this.bindingNavigatorSeparator2.Name = "bindingNavigatorSeparator2";
        this.bindingNavigatorSeparator2.Size = new System.Drawing.Size(6, 25);
        // 
        // bindingNavigatorSaveItem
        // 
        this.bindingNavigatorSaveItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
        this.bindingNavigatorSaveItem.Enabled = false;
        this.bindingNavigatorSaveItem.Name = "bindingNavigatorSaveItem";
        this.bindingNavigatorSaveItem.Size = new System.Drawing.Size(23, 22);
        this.bindingNavigatorSaveItem.Text = "Save Data";
        // 
        // dataGridViewTextBoxColumn3
        // 
        this.dataGridViewTextBoxColumn3.DataPropertyName = "SomeVal";
        this.dataGridViewTextBoxColumn3.HeaderText = "SomeVal";
        this.dataGridViewTextBoxColumn3.Name = "dataGridViewTextBoxColumn3";
        // 
        // dataGridViewTextBoxColumn4
        // 
        this.dataGridViewTextBoxColumn4.DataPropertyName = "Var";
        this.dataGridViewTextBoxColumn4.HeaderText = "Var";
        this.dataGridViewTextBoxColumn4.Name = "dataGridViewTextBoxColumn4";
        // 
        // simpleDataItemBindingSource
        // 
        this.simpleDataItemBindingSource.DataSource = typeof(SimpleDataItem);
        // 
        // Form1
        // 
        this.ClientSize = new System.Drawing.Size(375, 266);
        this.Controls.Add(this.dataGridView1);
        this.Controls.Add(this.simpleDataItemBindingNavigator);
        this.Name = "Form1";
        ((System.ComponentModel.ISupportInitialize)(this.simpleDataItemDataGridView)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.simpleDataItemBindingNavigator)).EndInit();
        this.simpleDataItemBindingNavigator.ResumeLayout(false);
        this.simpleDataItemBindingNavigator.PerformLayout();
        ((System.ComponentModel.ISupportInitialize)(this.simpleDataItemBindingSource)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();

    }


    private System.Windows.Forms.DataGridView simpleDataItemDataGridView;
    private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;
    private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;
    private System.Windows.Forms.DataGridViewTextBoxColumn SomeVal;
    private System.Windows.Forms.DataGridViewTextBoxColumn Var;
    private System.Windows.Forms.BindingSource simpleDataItemBindingSource;
    private System.Windows.Forms.DataGridView dataGridView1;
    private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn3;
    private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn4;
    private System.Windows.Forms.BindingNavigator simpleDataItemBindingNavigator;
    private System.Windows.Forms.ToolStripButton bindingNavigatorAddNewItem;
    private System.Windows.Forms.ToolStripLabel bindingNavigatorCountItem;
    private System.Windows.Forms.ToolStripButton bindingNavigatorDeleteItem;
    private System.Windows.Forms.ToolStripButton bindingNavigatorMoveFirstItem;
    private System.Windows.Forms.ToolStripButton bindingNavigatorMovePreviousItem;
    private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator;
    private System.Windows.Forms.ToolStripTextBox bindingNavigatorPositionItem;
    private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator1;
    private System.Windows.Forms.ToolStripButton bindingNavigatorMoveNextItem;
    private System.Windows.Forms.ToolStripButton bindingNavigatorMoveLastItem;
    private System.Windows.Forms.ToolStripSeparator bindingNavigatorSeparator2;
    private System.Windows.Forms.ToolStripButton bindingNavigatorSaveItem;


    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}
class SimpleDataItem
{
    private int m_SomeVal;

    public int SomeVal
    {
        get { throw new ArgumentException("foo"); }
        set { m_SomeVal = value; }
    }

    private string m_SomeVar;

    public string Var
    {
        get { return m_SomeVar; }
        set { m_SomeVar = value; }
    }
}








32.52.DataGrid
32.52.1.Load data in DataTable to DataGridLoad data in DataTable to DataGrid
32.52.2.Link two DataTable in a DataGridLink two DataTable in a DataGrid
32.52.3.Load Data to DataGridLoad Data to DataGrid
32.52.4.DataGrid View: on data error
32.52.5.Data binding for Multiple Controls
32.52.6.Binding DataSet to DataGrid
32.52.7.Programmatic Data Display