【原创】DataView 使用技巧

一 读取某个记录的详细信息

public  DataView Dv;

    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        Dv 
=   new  Caicai.DBHelper().ExecuteDataSet( " select * from News " ).Tables[ 0 ].DefaultView;
        Dv.RowFilter 
=   " id=2 " ;
    }

前台调用

< form id = " form1 "  runat = " server " >
    
< div >
        
<%   =  Dv[ 0 ][ 0 ].ToString()  %>  \  <%   =  Dv[ 0 ][ " Title " ].ToString()  %>  \  <%   =  Dv[ 0 ].Row[ " Content " ].ToString()  %>   < br  />
    
div >
    
form >


二 和GridView的配合使用

Caicai.DBHelper MyHelper  =   new  Caicai.DBHelper();
        DataSet Ds 
=  MyHelper.ExecuteDataSet( " select * from News " );

        DataView Dv1 
=  Ds.Tables[ 0 ].DefaultView;
        Dv1.RowFilter 
=   " id=1 " ;
        
this .GridView1.DataSource  =  Dv1;
        
this .GridView1.DataBind(); 
  
        DataView Dv2 
=  Ds.Tables[ 0 ].DefaultView;
        Dv2.RowFilter 
=   " id=2 " ;
        
this .GridView2.DataSource  =  Dv2;
        
this .GridView2.DataBind();

三 读取xml里的信息

DataSet Ds  =   new  DataSet();
        Ds.ReadXml(Server.MapPath(
@" App_Data/XMLFile.xml " ));
        DataView Dv 
=  Ds.Tables[ 0 ].DefaultView;
        Dv.RowFilter 
=   " id='11' " ;
        Response.Write(Dv[
0 ][ " title " ].ToString());

xml文件
< Root >
  
< Catalog title = " 菜菜灰 "  id = " 2 " />
  
< Catalog title = " caicaihui "  id = " 11 " />
Root >

转载于:https://www.cnblogs.com/caicaihui/archive/2007/12/06/984978.html

你可能感兴趣的:(【原创】DataView 使用技巧)