RadCombobox SelectedIndexChanged property

2 posts, 0 answers
  1. Anagha
    Anagha avatar
    1 posts
    Member since:
    Jun 2011

    Posted 10 Jun 2011 Link to this post

    Hello Telerik Team

    I'm using RadControl version : 2011.1.315.35 (for ASP.NET AJAX). I am using the Radgrid control also.
    I have a simple problem. I just want to load record in Radgrid control on selection of a value in Radcombobox.
    Here is the code,

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using Telerik.Web.UI;

    public partial class GridDemo2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            using (SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString))
            {
                using (SqlDataAdapter sda1 = new SqlDataAdapter())
                {
                    string qry = "select * from Categories";
                    sda1.SelectCommand = new SqlCommand(qry, con1);
                    DataTable dt = new DataTable();

                    con1.Open();
                    try
                    {
                        sda1.Fill(dt);
                        RadGrid1.DataSource = dt;
                    }
                    catch (Exception ex) {
                    }
                    finally
                    {
                        con1.Close();
                    }
                }
            }
        }
        protected void RadComboBox1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (IsPostBack)
            {
                using (SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString))
                {                
                  using (SqlDataAdapter sda1 = new SqlDataAdapter())
                {
                  string qry = "select * from Categories where Category_Name = '" + RadComboBox1.SelectedValue.ToString() + "'";
                    sda1.SelectCommand = new SqlCommand(qry, con1);
                    DataTable dt = new DataTable();

                    con1.Open();
                    try
                    {
                        sda1.Fill(dt);
                        RadGrid1.DataSource = dt;
                    }
                    catch (Exception ex) {
                    }
                    finally
                    {
                        con1.Close();
                    }
                }

            }
        }
    }


    In this example I have taken a radcombobox which is bind to Categories table and datafield value is category name.
    I want to fill the rad grid when radcombobox value is selected. I am passing that value in the query and on that query i want to load that 
    record in the radgrid. I am not able to bind the data to radgrid.

    If I am wrong in coding then please help me out.

    Thanks in advance.

    Regards
    Anagha






  2. Dimitar Terziev
    Admin
    Dimitar Terziev avatar
    1268 posts

    Posted 14 Jun 2011 Link to this post

    Hello Anagha,

    The reason for the experienced behavior is the fact that you should call the DataBind() method of the RadGird.Currently you are just assigning a data source to the RadGrid, but then nothing is causing the grid to bind to this data source.

    Change your event handling function to the following:
    protected void RadComboBox1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
       {
           if (IsPostBack)
           {
               using (SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString))
               {               
                 using (SqlDataAdapter sda1 = new SqlDataAdapter())
               {
                 string qry = "select * from Categories where Category_Name = '" + RadComboBox1.SelectedValue.ToString() + "'";
                   sda1.SelectCommand = new SqlCommand(qry, con1);
                   DataTable dt = new DataTable();
     
                   con1.Open();
                   try
                   {
                       sda1.Fill(dt);
                       RadGrid1.DataSource = dt;
                       RadGrid1.DataBind();
                   }
                   catch (Exception ex) {
                   }
                   finally
                   {
                       con1.Close();
                   }
               }
           }
       }

    I hope this would help you out.

    Best wishes,
    Dimitar Terziev
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Back to Top