【问题标题】:Unable to loading data into gridview无法将数据加载到gridview
【发布时间】:2013-07-25 00:48:05
【问题描述】:

我正在使用 WCF 服务将数据显示到 gridview 中,但显示不正确(列显示无序)

以下是我的单独类文件,其中包含要添加到 IList 对象的属性 代码:

    Class1.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace ServiceTest
    {
        public class Class1
        {
            public int index { get; set; }
            public string name { get; set; }
            public int id { get; set; }
        }
    }

这是服务接口

IService1.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Collections;

namespace ServiceTest
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {


        [OperationContract]
      IList<Class1> GetD();

    }
}

这是实现 IService.cs 的服务类 服务1 使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Runtime.Serialization; 使用 System.ServiceModel; 使用 System.ServiceModel.Web; 使用 System.Text; 使用 System.Collections;

namespace ServiceTest
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {


        public IList<Class1> GetD()
        {


            IList<Class1> lst = new List<Class1>();
            for (int i = 0; i < 50; i++)
            {
                Class1 c = new Class1();
                c.index = i;
                c.name = "Madhavi " + i;
                c.id = i + 1;
                lst.Add(c);
            }
            return lst;
        }

    }
}

下面是我的消费者代码,它有一个 gridview 数据绑定控件。 和消费者代码

 protected void Page_Load(object sender, EventArgs e)
    {
        //IList i = new ArrayList();
        Service1Client s = new Service1Client();
       // i = s.GetD();
        GridView1.DataSource = s.GetD();
        GridView1.DataBind();
    }

【问题讨论】:

  • 嗨,Imad 在您的服务中您使用的是IList。当您添加服务的引用时,您是否配置了服务返回类型List,默认情况下它是Array
  • 是的,我做到了。实际上相同的代码在其他电脑上运行正常,但不是我的。

标签: c# asp.net wcf gridview


【解决方案1】:

听起来好像您将AutoGenerateColumns 设置为true 用于GridView1。您可以尝试按照您希望这些列出现的顺序显式指定 .aspx 文件中的列,并将每列与数据源中的相应字段绑定。

<asp:GridView runat="server" id="GridView1">
     <Columns>
     </Columns>
</GridView>

【讨论】:

    猜你喜欢
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多