GridView计数记录超过阈值

时间:2018-12-14 21:59:11

标签: c# asp.net xml

我有一个读取一些XML的gridview,当记录大于10时我想计算qty字段,我将如何处理呢?

我的C#:

public partial class index : Page
{
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        loadgridbyxml();
    }
}

//load-data-from-XML
private void loadgridbyxml()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("myxml.xml"));
        GridView1.DataSource = ds;
        GridView1.DataMember = "YYY";
        GridView1.DataBind();
    }
}

我的XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<XXX>
  <YYY>
    <ZZZid="1">
      <postid>1</postid>
      <name>My Name</name>
      <email>test@test.com</email>
      <prod>My Product</prod>
      <rate>500</rate>
      <qty>11</qty>
    </ZZZ>
  <YYY>
<XXX>

1 个答案:

答案 0 :(得分:0)

看看以下代码是否有帮助:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            Index index = new Index(FILENAME);
            index.loadgridbyxml();
            DataTable results = index.ds.Tables["ZZZ"].AsEnumerable().Where(x => int.Parse(x.Field<string>("Qty")) > 10).CopyToDataTable();
        }
    }
    public class Index
    {
        public DataSet ds = null;
        string filename = "";
        public Index(string filename)
        {
            this.filename = filename;
        }

        public void loadgridbyxml()
        {
            ds = new DataSet();
            ds.ReadXml(filename);
        }
    }

}