Code/C#
[C#] DataSet, BindingSource, BindingNavigator, DataGridView.DataError, BindingSource.AddNew
Hide Code
2008. 12. 21. 19:47
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DataSetSample
{
public partial class Form1 : Form
{
private DataSet ourDataSet;
private DataTable ourDataTable;
private BindingSource ourBindingSource;
public Form1()
{
InitializeComponent();
InitializeMyComponent();
}
private void InitializeMyComponent()
{
ourDataSet = new DataSet("Memorizer");
ourDataTable = new DataTable("Cards");
ourDataSet.Tables.Add(ourDataTable);
ourDataTable.Columns.Add("Page", typeof(string));
ourDataTable.Columns["Page"].AllowDBNull = false;
this.components = new System.ComponentModel.Container();
ourBindingSource = new System.Windows.Forms.BindingSource(this.components);
ourBindingSource.DataSource = ourDataSet;
ourBindingSource.DataMember = "Cards";
this.dataGridView1.DataSource = ourBindingSource;
this.bindingNavigator1.BindingSource = ourBindingSource;
}
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
MessageBox.Show(e.Exception.Message);
}
private void buttonNew_Click(object sender, EventArgs e)
{
try
{
ourBindingSource.AddNew();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DataSetSample
{
public partial class Form1 : Form
{
private DataSet ourDataSet;
private DataTable ourDataTable;
private BindingSource ourBindingSource;
public Form1()
{
InitializeComponent();
InitializeMyComponent();
}
private void InitializeMyComponent()
{
ourDataSet = new DataSet("Memorizer");
ourDataTable = new DataTable("Cards");
ourDataSet.Tables.Add(ourDataTable);
ourDataTable.Columns.Add("Page", typeof(string));
ourDataTable.Columns["Page"].AllowDBNull = false;
this.components = new System.ComponentModel.Container();
ourBindingSource = new System.Windows.Forms.BindingSource(this.components);
ourBindingSource.DataSource = ourDataSet;
ourBindingSource.DataMember = "Cards";
this.dataGridView1.DataSource = ourBindingSource;
this.bindingNavigator1.BindingSource = ourBindingSource;
}
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
MessageBox.Show(e.Exception.Message);
}
private void buttonNew_Click(object sender, EventArgs e)
{
try
{
ourBindingSource.AddNew();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}