?? controlorders.cs
字號:
else {
dtCustomers.Clear();
daCustomers.Fill(dsNorthwind, "CustomerOrders");
}
}
catch(SqlCeException e) {
// Error handling mechanism
//
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
NorthwindData.ShowErrors(e);
return;
}
catch(Exception e) {
// Error handling mechanism
//
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
MessageBox.Show(e.Message, "Northwind");
return;
}
selectedCustomerID = comboBoxCustomers.SelectedValue.ToString();
RefreshOrders();
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
}
private void ClearOrders() {
listViewOrders.Items.Clear();
}
private void ClearProducts() {
listViewProducts.Items.Clear();
}
// Refreshes the Products ListView
//
private void RefreshProducts() {
SqlCeCommand cmdProducts = null;
SqlCeDataReader drProducts = null;
SqlCeConnection cnNorthwind = NorthwindData.GetInstance().NorthwindConnection;
ListViewItem listViewItem = null;
string item;
string sql;
if (0 == listViewOrders.SelectedIndices.Count) {
return;
}
item = listViewOrders.FocusedItem.Text;
if (0 == item.Length) {
return;
}
sql = @"SELECT P.ProductName, O.UnitPrice, O.Quantity, O.Discount " +
@"FROM ""Order Details"" AS O " +
@"INNER JOIN Products AS P " +
@"ON O.ProductID = P.ProductID " +
@"WHERE OrderID = " + item + " " +
@"ORDER BY P.ProductName";
try {
cmdProducts = new SqlCeCommand(sql, cnNorthwind);
drProducts = cmdProducts.ExecuteReader();
int fieldCount = drProducts.FieldCount;
while(drProducts.Read()) {
listViewItem = new ListViewItem("");
for (int i = 0; i < fieldCount; ++i) {
if (!drProducts.IsDBNull(i)) {
item = drProducts.GetValue(i).ToString();
}
else {
item = "";
}
if (0 == i) {
listViewItem.Text = item;
}
else {
listViewItem.SubItems.Add(item);
}
}
listViewProducts.Items.Add(listViewItem);
}
}
catch(SqlCeException e) {
// Error handling mechanism
//
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
NorthwindData.ShowErrors(e);
return;
}
catch(Exception e) {
// Error handling mechanism
//
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
MessageBox.Show(e.Message, "Northwind");
return;
}
finally {
drProducts.Close();
}
}
// Refreshes the Orders ListView
//
private void RefreshOrders() {
SqlCeCommand cmdOrders = null;
SqlCeDataReader drOrders = null;
SqlCeConnection cnNorthwind = NorthwindData.GetInstance().NorthwindConnection;
ListViewItem listViewItem = null;
string item;
string sql;
sql = @"SELECT O.OrderID, O.OrderDate, O.RequiredDate, O.ShippedDate, S.CompanyName " +
@"FROM Orders AS O " +
@"INNER JOIN Shippers AS S " +
@"ON O.ShipVia = S.ShipperID " +
@"WHERE CustomerID = '" + selectedCustomerID + "' " +
@"ORDER BY O.OrderDate DESC";
try {
cmdOrders = new SqlCeCommand(sql, cnNorthwind);
drOrders = cmdOrders.ExecuteReader();
int fieldCount = drOrders.FieldCount;
while(drOrders.Read()) {
listViewItem = new ListViewItem("");
for (int i = 0; i < fieldCount; ++i) {
if (!drOrders.IsDBNull(i)) {
if ("DateTime" == drOrders.GetDataTypeName(i)) {
item = drOrders.GetDateTime(i).ToShortDateString();
}
else {
item = drOrders.GetValue(i).ToString();
}
}
else {
item = "";
}
if (0 == i) {
listViewItem.Text = item;
}
else {
listViewItem.SubItems.Add(item);
}
}
listViewOrders.Items.Add(listViewItem);
}
}
catch(SqlCeException e) {
// Error handling mechanism
NorthwindData.ShowErrors(e);
return;
}
catch(Exception e) {
// Error handling mechanism
MessageBox.Show(e.Message, "Northwind");
return;
}
finally {
drOrders.Close();
}
}
// Sets the selectedCustomerID for the SQL query inside the RefreshOrders method.
//
private void comboBoxCustomers_SelectedIndexChanged(object sender, System.EventArgs e) {
// Note, the selectedCustomerID cannot be empty, which is by design.
//
if (!selectedCustomerID.Equals(comboBoxCustomers.SelectedValue) && 0 < selectedCustomerID.Length) {
selectedCustomerID = comboBoxCustomers.SelectedValue.ToString();
refreshOrders = true;
ClearOrders();
RefreshOrders();
ClearProducts();
refreshOrders = false;
}
}
private void listViewOrders_SelectedIndexChanged(object sender, System.EventArgs e) {
if (!refreshOrders) {
ClearProducts();
RefreshProducts();
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -