1

My AJAX-enabled WCF service returns JSON using this contract,

[DataContract]
public class JQGridContract
{
    [DataContract]
    public class Row
    {
        [DataMember]
        public int id { get; set; }

        [DataMember]
        public List<string> cell { get; set; }

        public Row()
        {
            cell = new List<string>();
        }
    }

    [DataMember]
    public int page { get; set; }

    [DataMember]
    public int total { get; set; }

    [DataMember]
    public int records { get; set; }

    [DataMember]
    public List<Row> rows { get; set; }

    public JQGridContract()
    {
        rows = new List<Row>();
    }
}  

So each row of data in the results is untyped -- a row is essentially just a List without any column names attached.

I think that makes it impossible for me to use the 'jsonmap' attribute of colModel? Basically I am retrieving a DataTable from the database, and then putting that DataTable into this JQGridContract form. Column information from the DataTable isnt contained in the json passed to the client, though.

Id like to be able to map a column of my underlying DataTable to a column of my jqGrid, but without the need to strongly type my data contract. Is this possible? I thought it might be using anonymous types, where a list of anonymous objects (each anonymous object being a row) that have properties corresponding to each column in the underlying DataTable, but I haven't been able to make that work.

Thanks.

EDIT

Here is an example of want I want to achieve (using server-side code, rather than javascript).

Below is essentially a column model for a jqGrid thats done in c#:

            return new JQGridColumnCollection()
            {
                new JQGridColumn()
                {
                    DataField = "ID",     // maps to the DataTable
                    DataType = typeof(int),
                    HeaderText = "ID",
                    PrimaryKey = true,
                },
                new JQGridColumn()
                {
                    DataField = "Name",  
                    DataType = typeof(string),
                    HeaderText = "Name"
                },
                new JQGridColumn()
                {
                    DataField = "Birthdate",  
                    DataType = typeof(DateTime),
                    HeaderText = "Birth Date" 
                }                
            };

The 'DataField' property of each column maps that column to a column in the underyling DataTable. The order of the columns in the DataTable could be different:

DataTable table = GetDataTable(" SELECT [Birthdate], [ID], [Name] From PersonTable "); 

But regardless of how I query my database, the grid will still show up where the first column is ID, the second column is Name, and the third column is Birthdate. I dont have to change my SQL query in order to change the order of the columns in my grid.

I essentially want the equivalent of a DataField property in my client-side colModel for the jqGrid. That would require that my JSON columns are named, or that I can atleast map a jqGrid column to the numerical index of a column in the JSON data source.

Sean Thoman
  • 7,429
  • 6
  • 56
  • 103

1 Answers1

1

The JQGridContract class which you use will get the data in the format which can be read by the standard jsonReader (see here for details). Each row of data in the results is not "untyped". It has the type "string". There are no needs to use jsonmap. The position of the string in the row defines to which column of grid the string belong. So for the data mapping the position in the cell list should be used.

If you use the JQGridContract class you don't need any strongly type data conversion. You can easy convert any data type to the string and so the data of your database table to JQGridContract instance. If you do will have problems you should append your question with the colModel definition of the jqGrid which you use.

UPDATED: It doesn't matter in which order you use fields in the SELECT. It is only important in which order you place the data from the table variable to the instance of JQGridContract. You have a method, for example, GetUserBirthday which returns JQGridContract. The method should place in the cell list the ID converted to string first, then the Name and then the Birthdate converted in the ISO date format (yyy-mm-dd). If you want use the JQGridContract which you defines you should do this.

By the way the DataTable in not the best way to get the data per SELECT. More effectively to use SqlCommand and SqlDataReader.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Is there anyway, for example, to say that the first position (first column) of each JSON row should be placed in the last column of the actual jqGrid? So I can move around the placement of each column in the presentation independently of how it is arranged in the JSON thats sent to the client? I thought that is what jsonmap did but I'm not sure. – Sean Thoman Jun 15 '11 at 21:38
  • I just noticed in another post you mentioned the 'remapColumns' function, maybe Ill try that. – Sean Thoman Jun 15 '11 at 21:47
  • @Sean Thoman: Where you think to give the information about the column mapping for the client? I can't imagine an example why it can be needed. Probably you describe an example which you has? In the standard case the jqGrid call one server method, for example, `GetProducts`. Both the grid column definition and the `GetProducts` has the same order of columns in the `cell` list. So no mapping are needed. Probably you have some situation like described [here](http://stackoverflow.com/questions/5383847/is-it-possible-to-modify-the-colmodel-after-data-has-loaded-with-jqgrid/5408195#5408195)? – Oleg Jun 15 '11 at 21:47
  • I just want the order of the columns in the jqGrid to be independent from the order of the columns in the JSON. That means I can change the way my jqGrid looks (re-arrange columns) without having to tamper with my data-access layer (my sql queries and/or wcf service). I don't want the arrangement of columns coming from the WCF service to dictate the arrangement of columns in the grid, if thats possible. – Sean Thoman Jun 15 '11 at 21:54
  • @Sean: Sorry, but it is not an example. If there are a misunderstanding one should clear all on an example. Currently I don't see any sense in what you wrote. The call of the client part (jqGrid) the server should use **one contract** used by both side. If we use the same letters, but I would write German words and you will read the word as English words you will nothing understand. If the server send the data in one fixed order the client have to know the column order and use it. – Oleg Jun 15 '11 at 22:05
  • @Sean: I updated my answer. Additionally it seems that you use not free open source jqGrid, but the commercial version instead. In the case you should use `jqgrid-asp.net` tag. The tag `jqgrid` is for the free open source version. Moreover http://www.trirand.net/forum/ is the best place for the questions about commercial version. – Oleg Jun 15 '11 at 22:46
  • Actually I dont use the commercial version, I tested the trial but I havent bought it, hence I am sticking to the free version. – Sean Thoman Jun 15 '11 at 22:49
  • @Sean: I bought commercial version only to support the developer, but I use myself only the free open source version. So I know only the free version and can help only in this one. – Oleg Jun 15 '11 at 22:53
  • So I think if I want to be able to change the ordering of columns on the client, I have to actually have the client pass the desired ordering to the server. Right? That should be good enough, but is that a common practice.. – Sean Thoman Jun 15 '11 at 22:59
  • @Sean: No. The are two things: the order of the data for the columns in the JSON data transferred from the server to the client and the order of the column displayed. Why you mix the things? If you want change the display order of the column you could use `remapColumns` method. It seems to me that you are still in investigation phase and not in the implementation. The usage of `remapColumns` are needed **really seldom**. In no real customer project I needed it. – Oleg Jun 15 '11 at 23:07
  • @Sean: In real projects both the WCF and the corresponding HTML/ASPX pages which defines jqGrid belong to **one site**. The [same origin policy](http://en.wikipedia.org/wiki/Same_origin_policy) push you to do this. So both jqGrid and the corresponding WCF method are the parts of one thing. If you change one you change the other. If you change the SELECT statement in one part of your code you change the other part of your code which get the data from the SELECT result. With jqGrid client and the corresponding WFC method it is the same. – Oleg Jun 15 '11 at 23:33