📄 14.3.txt
字号:
Listing 14.3 Controlling Views with Row Filters
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace _10_DataViews
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dgProducts;
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
private System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
private System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private System.Data.SqlClient.SqlCommand sqlSelectCommand2;
private System.Data.SqlClient.SqlCommand sqlInsertCommand2;
private System.Data.SqlClient.SqlCommand sqlUpdateCommand2;
private System.Data.SqlClient.SqlCommand sqlDeleteCommand2;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter2;
private _10_DataViews.ProductCategoriesDS productCategoriesDS1;
private System.Windows.Forms.Label label1;
private System.Data.DataView dataView1;
private System.Windows.Forms.ComboBox cbCategories;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ComboBox cbRowState;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
// fill rowstate combo box items
foreach( string state in Enum.GetNames( typeof(DataViewRowState) ))
{
cbRowState.Items.Add( state );
}
sqlDataAdapter1.Fill( productCategoriesDS1, “Products” );
sqlDataAdapter2.Fill( productCategoriesDS1, “Categories” );
// sort by prodcut name
dataView1.Sort = “ProductName ASC”;
}
private void SetFilters()
{
int curCatID = Convert.ToInt32(cbCategories.SelectedValue);
dataView1.RowFilter = “CategoryID=” + curCatID;
if( cbRowState.SelectedIndex != -1 )
dataView1.RowStateFilter = (DataViewRowState)
Enum.Parse( typeof(DataViewRowState),
cbRowState.SelectedItem.ToString(), true );
dgProducts.DataSource = dataView1;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
// …
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void cbCDategories_SelectedIndexChanged(
object sender,
System.EventArgs e)
{
SetFilters();
}
private void Form1_Closing(
object sender,
System.ComponentModel.CancelEventArgs e)
{
if( productCategoriesDS1.HasChanges() )
{
DialogResult result;
result = MessageBox.Show( this,
“Would you like to save your changes?”,
“Northwind Products”, MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question );
if( result == DialogResult.Cancel )
{
e.Cancel = true;
return;
}
else if( result == DialogResult.No )
{
return;
}
else
{
sqlDataAdapter1.Update( productCategoriesDS1 );
}
}
}
private void cbRowState_SelectedIndexChanged(
object sender,
System.EventArgs e)
{
SetFilters();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -