productsform.cs
来自「微软(Microsoft)出版社C井练习文件及解答」· CS 代码 · 共 111 行
CS
111 行
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ProductsMaintenance
{
public partial class ProductsForm : Form
{
public ProductsForm()
{
InitializeComponent();
}
private void ProductsForm_Load(object sender, EventArgs e)
{
// 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);
// 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.NumProductsTable' table. You can move, or remove it, as needed.
this.numProductsTableTableAdapter.Fill(this.northwindDataSet.NumProductsTable);
}
private void productsGrid_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
int newInteger;
productsGrid.Rows[e.RowIndex].ErrorText = "";
if ((productsGrid.Columns[e.ColumnIndex].DataPropertyName == "UnitsInStock") ||
(productsGrid.Columns[e.ColumnIndex].DataPropertyName == "UnitsOnOrder") ||
(productsGrid.Columns[e.ColumnIndex].DataPropertyName == "ReorderLevel"))
{
if (!int.TryParse(e.FormattedValue.ToString(), out newInteger) ||
newInteger < 0)
{
productsGrid.Rows[e.RowIndex].ErrorText =
"Value must be a non-negative number";
e.Cancel = true;
}
}
}
private void productsGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
productsGrid.Rows[e.RowIndex].ErrorText = "";
}
private void productsGrid_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
productsGrid.Rows[e.RowIndex].ErrorText = "Invalid input, please re-enter";
e.Cancel = true;
}
private void saveButton_Click(object sender, EventArgs e)
{
try
{
NorthwindDataSet changes = (NorthwindDataSet)northwindDataSet.GetChanges();
if (changes == null)
{
return;
}
DataTable dt = changes.Tables["Products"];
DataRow[] badRows = dt.GetErrors();
if (badRows.Length == 0)
{
int numRows = productsTableAdapter.Update(changes);
MessageBox.Show("Updated " + numRows + " rows", "Success");
northwindDataSet.AcceptChanges();
}
else
{
string errorMsg = null;
foreach (DataRow row in badRows)
{
foreach (DataColumn col in row.GetColumnsInError())
{
errorMsg += row.GetColumnError(col) + "\n";
}
}
MessageBox.Show("Errors in data: " + errorMsg,
"Please fix", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Errors",
MessageBoxButtons.OK, MessageBoxIcon.Error);
northwindDataSet.RejectChanges();
}
}
//private void queryButton_Click(object sender, EventArgs e)
//{
// NorthwindDataSetTableAdapters.ProductsTableAdapter productsTA =
// new NorthwindDataSetTableAdapters.ProductsTableAdapter();
// productsTA.Fill(northwindDataSet.Products);
// BindingSource productsBS = new BindingSource(northwindDataSet, "Products");
// productsGrid.DataSource = productsBS;
//}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?