Silverlight中DataGrid控件动态生成列并结合DataPager进行分页

来源:互联网 发布:vue.js 侧边栏导航 编辑:程序博客网 时间:2024/06/05 04:45
1、准备一个实体类 using System; using System.Collections.Generic; using System.Linq; using System.Web; ///summary ///电流数据的摘要说明
1、准备一个实体类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
///电流数据 的摘要说明
/// </summary>
public class DL
{
    public int ID {get; set; }
    public int 泵站ID {get; set; }
    public string 机组编号 {get; set; }
    public decimal 电流 {get; set; }
    public DateTime 时间 {get; set; }
}

  2、编写一个WebService,名称为:getBZInfo.asmx,提供给Silverlight应用程序使用,getBZInfo.cs文件中的代码如下,很简单就是调用数据库访问类,返回一个实体类集合。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using USTC;
using System.Data;
using System.Text;
/// <summary>
///getBZInfo 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class getBZInfo : System.Web.Services.WebService
{
    DMBZ dm = new DMBZ();
    public getBZInfo()
    {
        //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();
    }

    #region 电流数据
    /// <summary>
    ///获取某个泵站下某个机组的电流数据
    /// </summary>
    /// <param name="bzid"></param>
    /// <param name="jzbh"></param>
    /// <returns></returns>
    [WebMethod(Description = " 获取某个泵站下某个机组的电流数据")]
    public DL[] getDLInfoByBH(string bzid,string jzbh)
    {
        List<DL> list = new List<DL>();
        string sql = "select * FROM 电流数据 where 泵站ID='" + bzid + "' and 机组编号='" + jzbh +"'";
        DataSet ds = dm.getsql(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DL item = new DL();
                item.泵站ID = int.Parse(bzid);
                item.机组编号 = jzbh;
                item.电流 = decimal.Parse(ds.Tables[0].Rows[i]["电流"].ToString());
                item.时间 = DateTime.Parse(ds.Tables[0].Rows[i]["时间"].ToString());
                //将数据添加到集合中去
                list.Add(item);
            }
        }
        return list.ToArray();
    }
    #endregion
}

  3、编译并生成asp.net项目后,右键getBZInfo.asmx,选择在浏览器中浏览,确保可以访问。

  4、在Silverlight项目中添加服务引用,发现并添加上面生成的服务,服务命名为bzService,添加成功以后,修改产生的配置文件:ServiceReferences.ClientConfig

<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="getBZInfoSoap" maxBufferSize="2147483647"
maxReceivedMessageSize=
"2147483647">
                    <security mode="None" />
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:1245/webservice/getBZInfo.asmx"
                binding=
"basicHttpBinding" bindingConfiguration="getBZInfoSoap"
                contract=
"bzService.getBZInfoSoap" name="getBZInfoSoap" />
        </client>
    </system.serviceModel>
</configuration>

  中的<endpoint address="http://localhost:1245/webservice/getBZInfo.asmx" binding="basicHttpBinding" bindingConfiguration="getBZInfoSoap" contract="bzService.getBZInfoSoap" name="getBZInfoSoap" />

  中的address修改成项目的相对路径,修改后如下:

  <endpoint address="../webservice/getBZInfo.asmx" binding="basicHttpBinding" bindingConfiguration="getBZInfoSoap" contract="bzService.getBZInfoSoap" name="getBZInfoSoap" />

原创粉丝点击