C#中DataGrid加载XML数据

来源:互联网 发布:pv uv 数据库 设计 编辑:程序博客网 时间:2024/05/18 02:05

 protected System.Web.UI.WebControls.DataGrid dgAddInfo;
  public string xmlFileName = "Zph.xml";
  private void Page_Load(object sender, System.EventArgs e)
  {
   //   DataGridDataBind();
   
   if(!IsPostBack)
   {
    DataLoad("ID") ;
   }
  }

  #region
  private void DataLoad( string SortString )
  {
   //string xmlFileName = @"c:/inetpub/wwwroot/zhaopinhui/Zph.xml " ;
   DataSet ds = new DataSet();
   FileStream FS = new FileStream( Server.MapPath(xmlFileName) ,FileMode.Open);
   ds.ReadXml(FS);
   if(ds.Tables.Count == 0)
   {
    ds.Tables.Add ( MakeZphTable() );
   }
   Trace.Warn("rowcount1=",Convert.ToString(ds.Tables[0].Rows.Count));

   DataView dv =new DataView(ds.Tables[0]);
   if(SortString.Length>0)
   {
    dv.Sort = SortString;
   }
   this.dgAddInfo.DataSource = dv;
   this.dgAddInfo.DataBind();
   FS.Close();
  }
  #endregion

 

#region  创建招聘会的表
  private DataTable MakeZphTable()
  {
   DataTable theTable = new DataTable("Zph");

   DataColumn theColumn1 = new DataColumn();
   theColumn1.DataType = System.Type.GetType("System.Int32");
   theColumn1.ColumnName = "ID";
   theTable.Columns.Add(theColumn1);

   DataColumn theColumn2 = new DataColumn();
   theColumn2.DataType = System.Type.GetType("System.Int32");
   theColumn2.ColumnName = "ZphID" ;
   theTable.Columns.Add(theColumn2);

   DataColumn theColumn3 = new DataColumn();
   theColumn3.DataType = System.Type.GetType("System.String");
   theColumn3.ColumnName = "Title";
   theTable.Columns.Add(theColumn3);

   DataColumn theColumn4 = new DataColumn();
   theColumn4.DataType = System.Type.GetType("System.String");
   theColumn4.ColumnName = "Content";
   theTable.Columns.Add(theColumn4);

   DataColumn theColumn5 = new DataColumn();
   theColumn5.DataType = System.Type.GetType("System.DateTime");
   theColumn5.ColumnName = "Date";
   theTable.Columns.Add(theColumn5);

   DataColumn theColumn6 = new DataColumn();
   theColumn6.DataType = System.Type.GetType("System.String");
   theColumn6.ColumnName = "Time";
   theTable.Columns.Add(theColumn6);

   DataColumn theColumn7 = new DataColumn();
   theColumn7.DataType = System.Type.GetType("System.String");
   theColumn7.ColumnName = "Num";
   theTable.Columns.Add(theColumn7);

   DataColumn theColumn8 = new DataColumn();
   theColumn8.DataType = System.Type.GetType("System.String");
   theColumn8.ColumnName = "Place";
   theTable.Columns.Add(theColumn8);
   return theTable;
  }