0
votes

I am using WCF service to display data into gridview but it not properly displaying(Columns are displaying out of order)

Following is my seperate class file containing properties to be added to IList object Code:

    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; }
        }
    }

This is Service interface

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();

    }
}

This is service class that implements IService.cs Service1 using System; 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 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;
        }

    }
}

Below is my consumer code having one gridview databound control. And consumer code

 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
Hi Imad in your service you are using IList.When you add reference of your service did you configure service return type List by default it is ArrayNeeraj Dubey
Yes, I did. Actually same code is running properly in other pc, but not mine.user2546623

1 Answers

0
votes

Sounds as if you have AutoGenerateColumns set to true for the GridView1. You can instead try explicitly specifying the columns in the .aspx file in the order that you would like these columns to appear and bind each column with the appropriate field from the Datasource.

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