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

Unable to Filter Grid when setting DataSource in Code behind

1 Answer 56 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Civbaron
Top achievements
Rank 1
Civbaron asked on 09 Apr 2010, 04:01 PM
Hi,

I was trying to get the example of simple filtering working that is located at this location http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/filtering/defaultcs.aspx. I was able to get it working without too much trouble. The issue is that when I removed the 'DataSourceID="SqlDataSource1' from the Grid and used code in the Page_Load method to set the Datasource of the grid the filtering did not work. I set the datasource to be a list of ints. I have also tried using the NeedDataSource event of the grid itself to populate the grid, but again the grid will be populated but when I go to filter it doesn't filter.  Here is sample of the code I am using to set the datasource of the grid:

SqlCommand sql =

new SqlCommand();

 

SqlDataAdapter da =

new SqlDataAdapter();

 

 

string connectionString = WebConfigurationManager.ConnectionStrings["SBF"].ToString();

 

SqlConnection con =

new SqlConnection(connectionString);

 

 

//ConfigurationManager.ConnectionStrings[connectionString]

 

con.ConnectionString = connectionString;

sql.CommandText =

@"select A from SSA";

 

sql.Connection = con;

da.SelectCommand = sql;

DataTable dt =

new DataTable();

 

 

List<int> ints = new List<int>();

 

da.Fill(dt);

 

for (int i = 0; i < dt.Rows.Count; i++)

 

{

ints.Add(

Convert.ToInt32(dt.Rows[i]["A"]));

 

 

}

RadGrid1.DataSource = ints;

I am using the 2009.3.1208.35 version of the Telerik.Web.UI.

Thanks,
Jonathan Simon

1 Answer, 1 is accepted

Sort by
0
Accepted
Yavor
Telerik team
answered on 15 Apr 2010, 07:40 AM
Hi Jonathan,

In order to make sure that the control's functionalities are executed as expected, it is necessary to bind the control by using NeedDataSource:

http://www.telerik.com/help/aspnet-ajax/grdadvanceddatabinding.html

or a datasource control:

http://www.telerik.com/help/aspnet-ajax/grddeclarativedatasource.html

Setting the datasource in any other event, or calling .DataBind() for the control directly would not allow it to function as expected.
I hope this information helps.

Sincerely yours,
Yavor
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Civbaron
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Share this question
or