This is a migrated thread and some comments may be shown as answers.

Binding RadGrid client side records not displaying after binding

0 Answers 40 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anwar
Top achievements
Rank 2
Anwar asked on 05 Jun 2012, 01:35 PM
Hi friends ,

In our application we are binding grid on client side.
On binding the grid the records are not displying.I am not getting what's wrong in our code.
Bellow is the code i have written,

-----------------test.aspx -----------------------
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">      
 function pageLoad(sender, eventArgs) {          
          
           var tableView = $find("<%= RadGrid1.ClientID %>").get_masterTableView();
           $find("<%= RadAjaxLoadingPanel1.ClientID %>").show("<%= RadGrid1.ClientID %>");
           PageMethods.GetData(0, tableView.get_pageSize(),
                tableView.get_sortExpressions().toString(), tableView.get_filterExpressions().toList(),
                    updateGrid);
                   
           PageMethods.GetCount(tableView.get_filterExpressions().toList(), updateVirtualItemCount);
       }
function updateGrid(result) {          
           var tableView = $find("<%= RadGrid1.ClientID %>").get_masterTableView();
           tableView.set_dataSource(result);
           tableView.dataBind();          
 
           $find("<%= RadAjaxLoadingPanel1.ClientID %>").hide("<%= RadGrid1.ClientID %>");
          
        }
 function updateVirtualItemCount(result)
        {
           var tableView = $find("<%= RadGrid1.ClientID %>").get_masterTableView();
           tableView.set_virtualItemCount(result);
        }
  function RadGrid1_Command(sender, args) {         
            args.set_cancel(true);                       
            var pageSize = sender.get_masterTableView().get_pageSize();
            var sortExpressions = sender.get_masterTableView().get_sortExpressions();
            var filterExpressions = sender.get_masterTableView().get_filterExpressions();           
            var currentPageIndex = sender.get_masterTableView().get_currentPageIndex();               
            if (args.get_commandName() == "Filter")
                currentPageIndex = 0;
            var sortExpressionsAsSQL = sortExpressions.toString();
            var filterExpressionsAsSQL = filterExpressions.toString();
            $find("<%= RadAjaxLoadingPanel1.ClientID %>").show("<%= RadGrid1.ClientID %>");
            PageMethods.GetData(currentPageIndex * pageSize, pageSize, sortExpressionsAsSQL, filterExpressions.toList(), updateGrid);         
          
        }
</script>

    </telerik:RadCodeBlock>

<telerik:RadScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server" />
<telerik:RadGrid ID="RadGrid1" EnableViewState="false" runat="server" AllowPaging="true"
            AllowSorting="True" AllowFilteringByColumn="true" GridLines="None">
            <ItemStyle Wrap="false" />
            <MasterTableView AllowMultiColumnSorting="true" TableLayout="Fixed">
                <Columns>
                    <telerik:GridBoundColumn DataField="EmployeeID" HeaderText="EmployeeID" HeaderStyle-Width="100px"
                        FilterControlWidth="50px" />
                    <telerik:GridBoundColumn DataField="ENAME" HeaderText="ENAME" />
                    <telerik:GridBoundColumn DataField="SALARY" HeaderText="SALARY" />
                </Columns>
            </MasterTableView>
            <PagerStyle AlwaysVisible="true" Mode="NumericPages" />
            <ClientSettings>
                <ClientEvents OnCommand="RadGrid1_Command"  />
            </ClientSettings>
        </telerik:RadGrid>
        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" />
-----------------test.aspx.cs -----------------------
[WebMethod]
    public static int GetCount(List<GridFilterExpression> filterExpressions)
    {
        int count = 0;

        using (SqlConnection connection =
            new SqlConnection(
                ConfigurationManager.ConnectionStrings["sqlConstr"].ConnectionString))
        {           

            StringBuilder sqlBuilder = new StringBuilder();
            sqlBuilder.Append("SELECT COUNT(*) FROM Employee");        
          
            SqlCommand command = new SqlCommand(sqlBuilder.ToString(),
                    connection);

            parameters.ForEach(
                delegate(DbParameter parameter) { if (parameter != null) command.Parameters.Add(parameter); });

            connection.Open();

            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                count = reader.GetInt32(0);
            }

            reader.Close();

        }
        return count;
    }

    [WebMethod]
    public static List<Employee> GetData(int startIndex, int maximumRows,
        string sortExpressions, List<GridFilterExpression> filterExpressions)
    {

        List<Employee> list = new List<Employee>();
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.AppendLine(" SELECT * FROM Employee");     
        sqlBuilder.AppendLine(" Order By EID ASC");
        using (SqlConnection connection =
            new SqlConnection(
                ConfigurationManager.ConnectionStrings["sqlConstr"].ConnectionString))
        {
            SqlCommand command = new SqlCommand(sqlBuilder.ToString(), connection);           
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            try
            {
                while (reader.Read())
                {
                    Employee newEmployee = new Employee();
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        switch (reader.GetName(i))
                        {
                            case "SALARY":
                                newEmployee.SALARY  = reader.GetInt32(i);
                                break;
                            case "ENAME":
                                newEmployee.ENAME  = reader.GetString(i);
                                break;
                            case "EID":
                                newEmployee.EID  = reader.GetString(i);
                                break;                           
                            default:
                                break;
                        }
                    }
                    list.Add(newEmployee);
                }
            }
            finally
            {
                reader.Close();
            }
        }
        return list;
    }

public class Employee
    {
        private string _EID;
        private string _ENAME;
        private int _SALARY;  

        public Employee()
        {

        }
        public string EID
        {
            get
            {
                return this._EID;
            }
            set
            {
                if ((this._EID != value))
                {
                    this._EID = value;
                }
            }
        }

        public string ENAME
        {
            get
            {
                return this._ENAME;
            }
            set
            {
                if ((this._ENAME != value))
                {
                    this._ENAME = value;
                }
            }
        }

        public int SALARY
        {
            get
            {
                return this._SALARY;
            }
            set
            {
                if ((this._SALARY != value))
                {
                    this._SALARY = value;
                }
            }
        }
    
    }

 

 Thanks in advance
  Anwar


No answers yet. Maybe you can help?

Tags
Grid
Asked by
Anwar
Top achievements
Rank 2
Share this question
or