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

RadCombobox SelectedIndexChanged property

1 Answer 103 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Anagha
Top achievements
Rank 1
Anagha asked on 10 Jun 2011, 07:34 AM
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






1 Answer, 1 is accepted

Sort by
0
Dimitar Terziev
Telerik team
answered on 14 Jun 2011, 02:38 PM
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.

Tags
ComboBox
Asked by
Anagha
Top achievements
Rank 1
Answers by
Dimitar Terziev
Telerik team
Share this question
or