Showing posts with label grid. Show all posts
Showing posts with label grid. Show all posts

Wednesday, July 17, 2013

How to fix Master Table always NULL in get_masterTableView() method in Telerik Grid

I encounter a very weird issue today when i implemented a new feature with Telerik grid in my app

since i only manipulate the telerik grid from the client side.  the following is the snippet of my javascript code.

  var gridMasterTable = $find("<%=leadGrid.ClientID%>").get_masterTableView();
            gridMasterTable.set_dataSource(new Array());
            gridMasterTable.dataBind();

I try to bind the grid with empty array to show the grid. unfortunately the application always stop and show the warming message to indicate the master table is null.. it means that the table does not exist in the grid.

after I google the web for the solution, this post really help me identify the cause of the issue

http://www.telerik.com/community/forums/aspnet-ajax/grid/find-quot-lt-gridresults-clientid-gt-quot-get-mastertableview-is-coming-back-null.aspx

the trick to solve this issue is that when you bind your grid in the client side. you must have the onCommand property set in the ClientSettings section



 <ClientSettings>
         <ClientEvents OnGridCreated="leadGrid_Created" OnCommand="Customer_Command"/>
 </ClientSettings>

 

Monday, March 26, 2012

How to use WCF to enable Telerik Grid Client side binding

I spend almost two days, finally i can use the WCF to retrieve the data for the rad grid control from Telerik


You must remember that Rad Grid client side databinding did not support WCF hosting on the cross domain.The WCF must running from the same domain as the applicaiton that using the rad grid.



The Html Code Snippet in the ASPX page


 <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" GridLines="None" 
AllowSorting="True" AutoGenerateColumns="false" PageSize="10">
               <MasterTableView DataKeyNames="MyID" ClientDataKeyNames="MyID" PageSize="5" AllowPaging="true" >
                <PagerStyle Mode="NextPrevAndNumeric" />
                <Columns>
                <telerik:GridBoundColumn DataField="MyID" HeaderText="MyID" DataType="System.Int32">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="CustomerName" HeaderText="CustomerName"
                        DataType="System.String">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Addressline1" HeaderText="Addressline1"
                        DataType="System.String">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="City" HeaderText="City"
                        DataType="System.String">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Prov" HeaderText="Prov"
                        DataType="System.String">
                    </telerik:GridBoundColumn>
                </Columns>
                <PagerStyle Mode="NumericPages" />
            </MasterTableView>
            <ClientSettings>
                <DataBinding  SelectMethod="GetDataUsingDataContract"
            Location="~/WCF/TCCIService.svc"  SortParameterType="Linq" FilterParameterType="Linq"></DataBinding>
            </ClientSettings>
                </telerik:RadGrid>




WCF Service Code Snippet


  [ServiceKnownType(typeof(MyObject))]
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MyService
    {
        [OperationContract]
        public ResultData GetDataUsingDataContract(int startRowIndex, int maximumRows,  
string sortExpression, string filterExpression)
        {
 
            MyDB LeaseRetensionDBAccess = new MyDB();
            List<MyObject> tObjectList = new List<MyObject>();
            using (SqlDataReader tempDataReader = LeaseRetensionDBAccess.LoadDataTable("CRM_Portfolio_Sel"))
            {
                if (tempDataReader != null && tempDataReader.HasRows)
                {
                    while (tempDataReader.Read())
                    {
                        MyObject tObject = new MyObject();
                        for (int i = 0; i < tempDataReader.FieldCount; i++)
                        {
                            if (tempDataReader.GetValue(i) != DBNull.Value)
                            {
                                SciUtil.DataMapper.SetPropertyValue(tObject, tempDataReader.GetName(i), 
tempDataReader.GetValue(i));
                            }
                        }
                        tObjectList.Add(tObject);
                    }
                    tempDataReader.Close();
                }
            }
            ResultData result = new ResultData();
            result.Data = tObjectList.Skip<MyObject>(startRowIndex)
.Take<MyObject>(maximumRows).ToList<MyObject>();
            result.Count = tObjectList.Count;
            return result;
        }
    }
}
 
here i use linq to filter the record for the paging. 
 
tObjectList.Skip<MyObject>(startRowIndex)
.Take<MyObject>(maximumRows).ToList<MyObject>();
 
the Configuration Setting in the Web.Config file
 
 <services>
      <!-- Note: the service name must match the configuration name for the service implementation. -->
      <service name="TabWebApp.WCF.MyService" behaviorConfiguration="MyServiceTypeBehaviors" >
        <!-- Add the following endpoint.  -->
        <!-- Note: your service must have an http base address to add this endpoint. -->
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
        <endpoint contract="TabWebApp.WCF.MyService" binding="webHttpBinding" 
 address="" behaviorConfiguration="JsonBehavior"></endpoint>
      </service>
    </services>
   
    <behaviors>
      <endpointBehaviors>
        <behavior name="JsonBehavior">
          <enableWebScript/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="MyServiceTypeBehaviors" >
          <!-- Add the following element to your service behavior configuration. -->
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors> 
 
 
the following configuration will enable the WCF to serialize the data as JSON format.
tthis setting will fix the data serialization issue.  
       <behavior name="JsonBehavior">
          <enableWebScript/>
        </behavior>