using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Text.RegularExpressions; using System.Windows.Forms; namespace AppliedDataBindingSample { public partial class ListUIForm : Form { public ListUIForm() { InitializeComponent(); } private void bindingNavigatorSaveItem_Click(object sender, EventArgs e) { if( this.Validate() ) { this.productsBindingSource.EndEdit(); this.productsTableAdapter.Update(this.northwindDataSet.Products); } else { System.Windows.Forms.MessageBox.Show(this, "Validation errors occurred.", "Save", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning); } } private void ListUIForm_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'northwindDataSet.Suppliers' table. You can move, or remove it, as needed. this.suppliersTableAdapter.Fill(this.northwindDataSet.Suppliers); // TODO: This line of code loads data into the 'northwindDataSet.Categories' table. You can move, or remove it, as needed. this.categoriesTableAdapter.Fill(this.northwindDataSet.Categories); // TODO: This line of code loads data into the 'northwindDataSet.Products' table. You can move, or remove it, as needed. this.productsTableAdapter.Fill(this.northwindDataSet.Products); } private void productsDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { // Don't format if value is null if( e.Value == null ) return; // Get DataGridView column DataGridViewColumn clm = this.productsDataGridView.Columns[e.ColumnIndex]; // If unit price column if( clm.DataPropertyName == "UnitPrice" ) { // Format data source value and concatenate with " Banana" // and pluralize if necessary string unitPrice = string.Format("{0:0.00 Banana}", e.Value); if( (decimal)e.Value != 1 ) unitPrice += "s"; e.Value = unitPrice; // Signal that we've formatted this value e.FormattingApplied = true; } } private void productsDataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e) { // Get DataGridView column DataGridViewColumn clm = this.productsDataGridView.Columns[e.ColumnIndex]; // If unit price column if( clm.DataPropertyName == "UnitPrice" ) { // Extract first number from value and convert to decimal string unitPrice = (string)e.Value; Match match = Regex.Match(unitPrice, @"\d+(.\d{1,2})?"); e.Value = decimal.Parse(match.Value); // Signal that we've applied parsed this value e.ParsingApplied = true; } } private void productsDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { // Don't format if value is null if( e.FormattedValue == null ) return; // Get DataGridView column DataGridViewColumn clm = this.productsDataGridView.Columns[e.ColumnIndex]; // If unit price column if( clm.DataPropertyName == "UnitPrice" ) { string unitPrice = (string)e.FormattedValue; // Check if unitPrice is a number Match match = Regex.Match(unitPrice, @"\d+(.\d{1,2})?"); // If not correctly formatted, show error and // prevent focus leaving cell decimal result; if( !decimal.TryParse(match.Value, out result) ) { MessageBox.Show( "Unit Price must be in this format: 0.00 Bananas"); e.Cancel = true; } } } } }