无法将数据加载到gridview中

时间:2013-07-25 08:22:52

标签: c# asp.net wcf gridview

我正在使用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();
    }

1 个答案:

答案 0 :(得分:0)

听起来好像AutoGenerateColumns的{​​{1}}设置为true。您可以尝试按照您希望这些列显示的顺序显式指定GridView1文件中的列,并使用数据源中的相应字段绑定每个列。

.aspx