2012-11-07 2 views
0

먼저 파일을 저장하지 않고도 C#의 TreeView 및 GridView에 대한 데이터 소스로 linq 쿼리의 결과를 사용하는 방법을 찾으려고합니다. 서버에서 다양한 옵션을 시도했지만 문서를 먼저 파일로 저장해야합니다. 누군가 나를 도울 수 있습니까? 코드는 다음과 같습니다.TreeView 또는 GridView에 대한 데이터 소스로 linq 쿼리를 사용하는 방법

XDocument products = new XDocument(
     new XDeclaration("1.0", "utf-8", ""), 
      new XElement("products", 
       new XElement("product", new XAttribute("id", "p1"), 
         new XElement("name", "Alpha"), 
         new XElement("Address", "200 WATERLOO DRIVE"), 
         new XElement("price", 
          new XElement("currency", "Rs.")), 
         new XElement("stock", "19"), 
         new XElement("country", "USA", 
          new XElement("state", "California"))), 
       new XElement("product", new XAttribute("id", "p2"), 
         new XElement("name", "Beta"), 
         new XElement("Address", "500 MOUNTBATTEN AVENUE"), 
         new XElement("price", 
          new XElement("currency", "Rs.")), 
         new XElement("stock", "25"), 
         new XElement("country", "USA", 
          new XElement("state", "Florida"))))); 
//create a linq query 
var newxml = from f1 in products.Elements("product") 
         where (string)f1.Element("country").Element("state") != "Florida" 
         select f1; 

//Create an xml document in memory using the linq query 
XDocument xdoc = new XDocument(
     new XDeclaration("1.0", "utf-8", ""), 
     new XElement("products")); 
xdoc.Element("products").Add(newxml); 

//create a datasource for TreeView or GridView using the xml document in memory. 
XmlDataSource xmlds = new XmlDataSource(); 
xmlds.DataFile=xdoc; 
TreeView1.DataSource = xmlds; 
TreeView1.DataBind(); 
GridView1.DataSource = xmlds; 
GridView1.DataBind(); 

xdoc에서 데이터 소스를 만드는 코드 부분이 작동하지 않습니다. 그것은 파일을 저장 한 다음 데이터 소스에 대한 파일을 호출하여 작동하도록 만들 수 있지만 메모리에서이 작업을 수행하려고합니다.

답변

0

linq 쿼리 결과를 데이터 소스로 직접 사용할 수 있습니다.

gridview1.datasource = newxml; gridview1.databind();

단점 나는 예를 들어 당신이 자신의 정렬 방법을 구현해야합니다 일을 정렬하는 것입니다 발생했습니다.

관련 문제