马牛不是人

学姐说:“学习学习不后退”

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
下面这段代码是一个完整的文件,原代码来自WROX.COM,演示了DataGrid的稍进一步的应用。
using System;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;

///    <summary>
///    This class provides    an example of creating and using a data    grid.
///    </summary>

public class DataSourceDataView : System.Windows.Forms.Form
{
    
private System.Windows.Forms.Button retrieveButton;
    
private System.Windows.Forms.DataGrid dataGrid;
    
private System.Windows.Forms.DataGrid dataGrid2;
    
private System.Windows.Forms.ComboBox comboBox1;

    
///    <summary>
    
///    Construct the window.
    
///    </summary>
    
///    <remarks>
    
///    This method    constructs the window by creating both the data    grid and the button.
    
///    </remarks>

    public DataSourceDataView()
    
{
        
/*        this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
                this.ClientSize    = new System.Drawing.Size (464,    253);
                this.Text =    "04_DataSourceDataView" ;
                this.dataGrid =    new    System.Windows.Forms.DataGrid ();
                dataGrid.BeginInit ();
                dataGrid.Location =    new    System.Drawing.Point (8, 8);
                dataGrid.Size =    new    System.Drawing.Size    (448, 208);
                dataGrid.TabIndex =    0;
                dataGrid.Anchor    = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right ;
                this.Controls.Add (this.dataGrid);
                dataGrid.EndInit ();
                this.retrieveButton    = new System.Windows.Forms.Button ();
                retrieveButton.Location    = new System.Drawing.Point (384, 224);
                retrieveButton.Size    = new System.Drawing.Size (75, 23);
                retrieveButton.TabIndex    = 1;
                retrieveButton.Anchor =    AnchorStyles.Bottom | AnchorStyles.Right ;
                retrieveButton.Text    = "Retrieve";
                retrieveButton.Click +=    new    System.EventHandler    (this.retrieveButton_Click);
                this.Controls.Add (this.retrieveButton); 
*/


        
this.AutoScaleBaseSize = new System.Drawing.Size(513);
        
this.ClientSize = new System.Drawing.Size(464493);
        
this.Text = "04_DataSourceDataView";
        
this.dataGrid = new System.Windows.Forms.DataGrid();
        dataGrid.BeginInit();
        dataGrid.CaptionText 
= "From Database";
        dataGrid.Font 
= new System.Drawing.Font("Tahoma"8);
        dataGrid.Location 
= new System.Drawing.Point(88);
        dataGrid.Size 
= new System.Drawing.Size(448208);
        dataGrid.TabIndex 
= 0;
        dataGrid.Anchor 
= AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
        
this.Controls.Add(this.dataGrid);
        dataGrid.EndInit();

        
this.retrieveButton = new System.Windows.Forms.Button();
        retrieveButton.Location 
= new System.Drawing.Point(8224);
        retrieveButton.Size 
= new System.Drawing.Size(7523);
        retrieveButton.TabIndex 
= 1;
        retrieveButton.Anchor 
= AnchorStyles.Top | AnchorStyles.Left;
        retrieveButton.Text 
= "Retrieve";
        retrieveButton.Click 
+= new System.EventHandler(this.retrieveButton_Click);
        
this.Controls.Add(this.retrieveButton);

        
this.comboBox1 = new System.Windows.Forms.ComboBox();
        comboBox1.Location 
= new System.Drawing.Point(96224);
        comboBox1.Size 
= new System.Drawing.Size(36021);
        comboBox1.DropDownStyle 
= ComboBoxStyle.DropDownList;
        comboBox1.TabIndex 
= 2;
        comboBox1.Anchor 
= AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
        comboBox1.SelectedIndexChanged 
+= new System.EventHandler(this.comboBox1_SelectedIndexChanged);
        comboBox1.Items.AddRange(
new object[8"Added""CurrentRows""Deleted""ModifiedCurrent""ModifiedOriginal""None""OriginalRows""Unchanged" });
        
this.Controls.Add(this.comboBox1);

        
this.dataGrid2 = new System.Windows.Forms.DataGrid();
        dataGrid2.BeginInit();
        dataGrid2.Location 
= new System.Drawing.Point(8256);
        dataGrid2.Size 
= new System.Drawing.Size(448232);
        dataGrid2.DataMember 
= "";
        dataGrid2.CaptionText 
= "Filtered";
        dataGrid2.Font 
= new System.Drawing.Font("Tahoma"8);
        dataGrid2.TabIndex 
= 3;
        dataGrid2.Anchor 
= AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
        
this.Controls.Add(this.dataGrid2);
        dataGrid2.EndInit();

    }


    
protected void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
    
{
        DataViewRowState state;

        
switch (comboBox1.Text)
        
{
            
case "Added":
                state 
= DataViewRowState.Added;
                
break;
            
case "CurrentRows":
                state 
= DataViewRowState.CurrentRows;
                
break;
            
case "Deleted":
                state 
= DataViewRowState.Deleted;
                
break;
            
case "ModifiedCurrent":
                state 
= DataViewRowState.ModifiedCurrent;
                
break;
            
case "ModifiedOriginal":
                state 
= DataViewRowState.ModifiedOriginal;
                
break;
            
case "None":
                state 
= DataViewRowState.None;
                
break;
            
case "OriginalRows":
                state 
= DataViewRowState.OriginalRows;
                
break;
            
case "Unchanged":
                state 
= DataViewRowState.Unchanged;
                
break;
            
default:
                state 
= DataViewRowState.OriginalRows;
                
break;
        }


        
try
        
{
            ((DataView)dataGrid2.DataSource).RowStateFilter 
= state;
        }

        
catch (Exception ex)
        
{
            System.Windows.Forms.MessageBox.Show(ex.ToString());

        }

    }


    
///    <summary>
    
///    Retrieve the data
    
///    </summary>
    
///    <param name="sender"> </param>
    
///    <param name="e"> </param>

    protected void retrieveButton_Click(object sender, System.EventArgs e)
    
{
        retrieveButton.Enabled 
= false;

        
string select = "SELECT * FROM MainInfo";

        OleDbConnection con 
= new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Manio\My Documents\PrintManager\program\PrintManageV1\DataBase\PrintDB.mdb");

        OleDbDataAdapter da 
= new OleDbDataAdapter(select, con);

        DataSet ds 
= new DataSet();

        da.Fill(ds, 
"MainInfo");

        dataGrid.SetDataBinding(ds, 
"MainInfo");

        DataView dv 
= new DataView(ds.Tables["MainInfo"]);

        dataGrid2.SetDataBinding(dv, 
null);

        comboBox1.Text 
= "OriginalRows";
    }


    
///    <summary>
    
///    Display    the    application    window
    
///    </summary>

    static void Main()
    
{
        Application.Run(
new DataSourceDataView());
    }

}
DataViewManager的用法:
DataViewManager创建时会同时创建一个DataSet中DataTable的DataView,根据过滤条件改变显示出来的行或者行的状态。即使不希望过滤数据,也可以把DataSet包装到DataViewManager中,以进行显示,因为这样在修改代码时可以使用更多的选项。

先来看一下DataViewManager的一个简单应用。下面的例子是本博客中另一篇文章《DataGrid实例(简单易懂,无复杂功能,适合初学者)》的一个实例改过来的,我们改用DataViewManager来显示数据,一看就明白。代码如下,和《DataGrid一文》不同之处已注明。
using System;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
//Professional C# 2nd的DATAGRID实例
///    <summary>
///    This class provides    an example of creating and using a data    grid.
///    </summary>

public class DisplayTabularData : System.Windows.Forms.Form
{
    
private System.Windows.Forms.Button retrieveButton;
    
private System.Windows.Forms.DataGrid dataGrid;

    
///    <summary>
    
///    Construct the window.
    
///    </summary>
    
///    <remarks>
    
///    This method    constructs the window by creating both the data    grid and the button.
    
///    </remarks>

    public DisplayTabularData()
    
{
        
this.AutoScaleBaseSize = new System.Drawing.Size(513);
        
this.ClientSize = new System.Drawing.Size(464253);
        
this.Text = "01_DisplayTabularData";
        
this.dataGrid = new System.Windows.Forms.DataGrid();
        dataGrid.BeginInit();
        dataGrid.Location 
= new System.Drawing.Point(88);
        dataGrid.Size 
= new System.Drawing.Size(448208);
        dataGrid.TabIndex 
= 0;
        dataGrid.Anchor 
= AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
        
this.Controls.Add(this.dataGrid);
        dataGrid.EndInit();
        
this.retrieveButton = new System.Windows.Forms.Button();
        retrieveButton.Location 
= new System.Drawing.Point(384224);
        retrieveButton.Size 
= new System.Drawing.Size(7523);
        retrieveButton.TabIndex 
= 1;
        retrieveButton.Anchor 
= AnchorStyles.Bottom | AnchorStyles.Right;
        retrieveButton.Text 
= "Retrieve";
        retrieveButton.Click 
+= new System.EventHandler(this.retrieveButton_Click);
        
this.Controls.Add(this.retrieveButton);
    }


    
///    <summary>
    
///    Retrieve the data
    
///    </summary>
    
///    <param name="sender"> </param>
    
///    <param name="e"> </param>

    protected void retrieveButton_Click(object sender, System.EventArgs e)
    
{
        retrieveButton.Enabled 
= false;

        
string source = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Manio\My Documents\PrintManager\program\PrintManageV1\DataBase\PrintDB.mdb";
        
string select = "SELECT * FROM MainInfo";

        
////////////////////////////////
        //填充DataSet的步骤
        
//1、使用数据库连接字符串创建数据库连接对象
        
//    2、用SQL查询语句和数据库连接对象创建数据库适配器dataAdapter
        
//        3、使用DataAdapter的Fill 方法填充DataSet

        OleDbConnection OleCon 
= new OleDbConnection(source);

        OleDbDataAdapter da 
= new OleDbDataAdapter(select,OleCon);

        DataSet ds 
= new DataSet();

        da.Fill(ds, 
"MainInfo");
        
////////////////////////////////////////////////////////////////////////////////////////
        
////////////////////////////////////////////////////////////////////////////////////////

        //不用之处
        ////////////////////////////////////////////////////////////////////////////////////////

        DataViewManager dvm 
= new DataViewManager(ds);

        dvm.DataViewSettings[
"MainInfo"].RowFilter = "D_ID=2";
        dataGrid.SetDataBinding(dvm, 
"MainInfo");    
        
////////////////////////////////////////////////////////////////////////////////////////
        
////////////////////////////////////////////////////////////////////////////////////////

    }


    
///    <summary>
    
///    Display    the    application    window
    
///    </summary>

    static void Main()
    
{
        Application.Run(
new DisplayTabularData());
    }

}
posted on 2006-03-31 22:46  马牛  阅读(317)  评论(0编辑  收藏  举报