DataView对象

DataView对象对象定义了DataTable的数据查看方式。数据默认的查看方式是将数据以表格的形式排列,且数据排列的顺序遵循从数据库表中取出数据时排列的顺序,也可以通过DataView对象使数据按照某些规则进行显示。
protected void Page_Load(object sender, EventArgs e)
    {
        OleDbConnection conn 
= new OleDbConnection();
        conn.ConnectionString 
= "Provider=Microsoft.Jet.OLEDB.4.0;" +
            
"Data Source=" + Server.MapPath("person.mdb");
        conn.Open();
        
string strSQL = "select * from grade";
        OleDbCommand Comm 
= new OleDbCommand(strSQL, conn);
        OleDbDataAdapter da
= new OleDbDataAdapter();
        da.SelectCommand 
= Comm;
        DataSet ds 
= new DataSet();
        da.Fill(ds, 
"grade");
        DataView dv 
= new DataView(ds.Tables["grade"]);
        dv.RowFilter 
= " 数学>80";
        dv.Sort 
= "学号 desc,数学 desc";
        Response.Write(
"满足条件的记录有:" + dv.Count + "");

        dg.DataSource 
= dv;
        dg.DataBind();
        conn.Close();
    }
通过DataView对象的Find()方法可以对数据进行查找,如果找到了,该方法将返回所在行的索引,并可以将查找到的数据输出。
protected void Page_Load(object sender, EventArgs e)
    {
        OleDbConnection conn 
= new OleDbConnection();
        conn.ConnectionString 
= "Provider=Microsoft.Jet.OLEDB.4.0;" +
            
"Data Source=" + Server.MapPath("person.mdb");
        conn.Open();
        
string strSQL = "select * from grade";
        OleDbCommand Comm 
= new OleDbCommand(strSQL, conn);
        OleDbDataAdapter da
= new OleDbDataAdapter();
        da.SelectCommand 
= Comm;
        DataSet ds 
= new DataSet();
        da.Fill(ds, 
"grade");
        DataView dv 
= new DataView(ds.Tables["grade"],"","姓名",DataViewRowState.CurrentRows);
        
int rowIndex = dv.Find("小张");
        
if (rowIndex == -1)
            Response.Write(
"没有找到!");
        
else
        {
            Response.Write(dv[rowIndex][
"姓名"].ToString() + dv[rowIndex]["数学"].ToString());
            dg.DataSource 
= dv;
            dg.DataBind();
        }
        conn.Close();
    }
posted @ 2007-05-24 10:23  齐心  Views(766)  Comments(0Edit  收藏  举报