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

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » ADO.Net » DataGrid 
32.52.4.DataGrid View: on data error
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(2732);
        this.simpleDataItemDataGridView.Name = "simpleDataItemDataGridView";
        this.simpleDataItemDataGridView.Size = new System.Drawing.Size(300220);
        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(2739);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(300220);
        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(00);
        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(37525);
        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(2322);
        this.bindingNavigatorAddNewItem.Text = "Add new";
        // 
        // bindingNavigatorCountItem
        // 
        this.bindingNavigatorCountItem.Name = "bindingNavigatorCountItem";
        this.bindingNavigatorCountItem.Size = new System.Drawing.Size(3622);
        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(2322);
        this.bindingNavigatorDeleteItem.Text = "Delete";
        // 
        // bindingNavigatorMoveFirstItem
        // 
        this.bindingNavigatorMoveFirstItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
        this.bindingNavigatorMoveFirstItem.Name = "bindingNavigatorMoveFirstItem";
        this.bindingNavigatorMoveFirstItem.Size = new System.Drawing.Size(2322);
        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(2322);
        this.bindingNavigatorMovePreviousItem.Text = "Move previous";
        // 
        // bindingNavigatorSeparator
        // 
        this.bindingNavigatorSeparator.Name = "bindingNavigatorSeparator";
        this.bindingNavigatorSeparator.Size = new System.Drawing.Size(625);
        // 
        // bindingNavigatorPositionItem
        // 
        this.bindingNavigatorPositionItem.Name = "bindingNavigatorPositionItem";
        this.bindingNavigatorPositionItem.Size = new System.Drawing.Size(5025);
        this.bindingNavigatorPositionItem.Text = "0";
        this.bindingNavigatorPositionItem.ToolTipText = "Current position";
        // 
        // bindingNavigatorSeparator1
        // 
        this.bindingNavigatorSeparator1.Name = "bindingNavigatorSeparator1";
        this.bindingNavigatorSeparator1.Size = new System.Drawing.Size(625);
        // 
        // bindingNavigatorMoveNextItem
        // 
        this.bindingNavigatorMoveNextItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
        this.bindingNavigatorMoveNextItem.Name = "bindingNavigatorMoveNextItem";
        this.bindingNavigatorMoveNextItem.Size = new System.Drawing.Size(2322);
        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(2322);
        this.bindingNavigatorMoveLastItem.Text = "Move last";
        // 
        // bindingNavigatorSeparator2
        // 
        this.bindingNavigatorSeparator2.Name = "bindingNavigatorSeparator2";
        this.bindingNavigatorSeparator2.Size = new System.Drawing.Size(625);
        // 
        // bindingNavigatorSaveItem
        // 
        this.bindingNavigatorSaveItem.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
        this.bindingNavigatorSaveItem.Enabled = false;
        this.bindingNavigatorSaveItem.Name = "bindingNavigatorSaveItem";
        this.bindingNavigatorSaveItem.Size = new System.Drawing.Size(2322);
        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(375266);
        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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.