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

webservice code for radgrid client binding

1 Answer 108 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dhamodharan
Top achievements
Rank 1
Dhamodharan asked on 06 Jan 2012, 11:59 AM
Hi,

I want to get record into radgrid when radcombobox client selected index changed. i want full code for ASMX page. please let me know how to bind the grid whlie radcomobox selected index changed using selected value.

Thanks,
Dhamu.

1 Answer, 1 is accepted

Sort by
0
Richard
Top achievements
Rank 1
answered on 06 Jan 2012, 04:26 PM
Dhamu:

There are comprehensive instructions for client-side binding of data to RadGrid using a WebService on the Client Binding to WCF Web Service/ADO.NET Data Service documentation page.

You can reference the ComboBox / Load on Demand modes demo at http://demos.telerik.com/aspnet-ajax/combobox/examples/populatingwithdata/autocompletesql/defaultcs.aspx for help with the webservice call code.

Products.asmx:
<%@ WebService Language="C#" CodeBehind="~/App_Code/Products.cs" Class="Products" %>
 
Products.cs:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
 
using Telerik.Web.UI;
 
 
[ScriptService]
public class Products : WebService
{
    [WebMethod]
    public RadComboBoxData GetCompanyNames(RadComboBoxContext context)
    {
        string sql = "SELECT * from Customers WHERE CompanyName LIKE @text + '%'";
 
        SqlDataAdapter adapter = new SqlDataAdapter(sql,
            ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
        DataTable data = new DataTable();
 
        adapter.SelectCommand.Parameters.AddWithValue("@text", context.Text);
        adapter.Fill(data);
 
        List<RadComboBoxItemData> result = new List<RadComboBoxItemData>(context.NumberOfItems);
        RadComboBoxData comboData = new RadComboBoxData();
        try
        {
 
            int itemsPerRequest = 10;
            int itemOffset = context.NumberOfItems;
            int endOffset = itemOffset + itemsPerRequest;
            if (endOffset > data.Rows.Count)
            {
                endOffset = data.Rows.Count;
            }
            if (endOffset == data.Rows.Count)
            {
                comboData.EndOfItems = true;
            }
            else
            {
                comboData.EndOfItems = false;
            }
            result = new List<RadComboBoxItemData>(endOffset - itemOffset);
            for (int i = itemOffset; i < endOffset; i++)
            {
                RadComboBoxItemData itemData = new RadComboBoxItemData();
                itemData.Text = data.Rows[i]["CompanyName"].ToString();
                itemData.Value = data.Rows[i]["CompanyName"].ToString();
             
                result.Add(itemData);
                }
 
            if (data.Rows.Count > 0)
            {
                comboData.Message = String.Format("Items <b>1</b>-<b>{0}</b> out of <b>{1}</b>", endOffset.ToString(), data.Rows.Count.ToString());
            }
            else
            {
                comboData.Message = "No matches";
            }
        }
        catch(Exception e)
        {
            comboData.Message = e.Message;
        }
     
        comboData.Items = result.ToArray();
        return comboData;
    }
}

And, there is also a ComboBox/Combo in Grid demo at http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/comboingrid/defaultcs.aspx?product=combobox to assist.

Hope this helps!
Tags
Grid
Asked by
Dhamodharan
Top achievements
Rank 1
Answers by
Richard
Top achievements
Rank 1
Share this question
or