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

Filtering does not happen

2 Answers 20 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David Russell
Top achievements
Rank 1
David Russell asked on 22 May 2010, 01:05 PM

The following code shows a Grid control hierarchy being created from the page Init function. Data is displayed along with filter controls but no filter action takes place when requested.

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.Odbc;  
using Telerik.Web.UI;  
 
namespace preTOPS  
{  
    public partial class Home : System.Web.UI.Page  
    {  
        OdbcConnection connection;  
 
        protected override void OnInit(EventArgs e)  
        {  
            base.OnInit(e);  
 
            connection = Master.connection;  
 
            try  
            {  
                if (Session["user"] == null)  
                    throw new Exception("@You must be logged in to view this page. Please <a href='Default.aspx' style='color:White; text-decoration:underline;'>login</a>.");  
 
                DefineGridStructure();  
            }  
            catch (Exception ex)  
            {  
                MyUtilities.DoError(Master.maindiv, Master.errordiv, Master.lblError, ex, connection, null, Master.GetUserName());  
            }  
        }  
 
        protected void Page_Load(object sender, EventArgs e)  
        {  
            if (!Master.maindiv.Visible)  
                return;  
 
            //RadGrid grid = (RadGrid)divgrid.FindControl("grid1");  
            //RadAjaxManager1.AjaxSettings.AddAjaxSetting(grid, grid);  
        }  
 
        private void DefineGridStructure()  
        {  
            RadGrid grid = new RadGrid();  
 
            grid.ID = "grid1";  
            grid.Width = Unit.Percentage(100);  
            grid.PageSize = 10;  
            grid.AllowPaging = true;  
            grid.AllowSorting = true;  
            grid.AllowFilteringByColumn = true;  
            grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;  
            grid.AutoGenerateColumns = false;  
            grid.GroupingEnabled = true;  
            grid.ShowGroupPanel = true;  
            grid.ShowStatusBar = true;  
            grid.ClientSettings.AllowDragToGroup = true;  
            grid.NeedDataSource += new GridNeedDataSourceEventHandler(grid_NeedIndications);  
            grid.MasterTableView.PageSize = 15;  
            grid.MasterTableView.DataKeyNames = new string[] { "indication" };  
 
            GridBoundColumn c = new GridBoundColumn();  
            c.DataField = "indication";  
            c.HeaderText = "Indication";  
            c.SortExpression = "indication";  
            c.UniqueName = "indication";  
            c.AllowSorting = true;  
            c.AllowFiltering = true;  
            c.DataType = System.Type.GetType("System.String");  
            grid.MasterTableView.Columns.Add(c);  
//          grid.MasterTableView.GroupByExpressions.Add(new GridGroupByExpression("Indication Group By Indication"));  
 
            GridTableView tvProtocols = new GridTableView(grid);  
            tvProtocols.Width = Unit.Percentage(100);  
            tvProtocols.DataKeyNames = new string[] { "protocol" };  
            //tvProtocols.AllowFilteringByColumn = true;  
            tvProtocols.AllowSorting = true;  
            tvProtocols.AllowMultiColumnSorting = true;  
            tvProtocols.AllowPaging = true;  
 
            c = new GridBoundColumn();  
            c.DataField = "protocol";  
            c.HeaderText = "Protocol";  
            c.SortExpression = "protocol";  
            c.UniqueName = "protocol";  
            c.AllowSorting = true;  
            c.AllowFiltering = true;  
            c.DataType = System.Type.GetType("System.String");  
            tvProtocols.Columns.Add(c);  
 
            c = new GridBoundColumn();  
            c.DataField = "sponsor";  
            c.HeaderText = "Sponsor";  
            c.UniqueName = "sponsor";  
            c.SortExpression = "sponsor";  
            c.AllowSorting = true;  
            c.AllowFiltering = true;  
            c.DataType = System.Type.GetType("System.String");  
            tvProtocols.Columns.Add(c);  
 
            c = new GridBoundColumn();  
            c.DataField = "shorttitle";  
            c.HeaderText = "Short Title";  
            c.AllowSorting = true;  
            c.AllowFiltering = true;  
            c.DataType = System.Type.GetType("System.String");  
            tvProtocols.Columns.Add(c);  
 
            c = new GridBoundColumn();  
            c.DataField = "cro";  
            c.HeaderText = "CRO";  
            c.AllowSorting = true;  
            c.AllowFiltering = true;  
            c.DataType = System.Type.GetType("System.String");  
            tvProtocols.Columns.Add(c);  
 
            GridRelationFields rf = new GridRelationFields();  
            rf.MasterKeyField = "indication";  
            rf.DetailKeyField = "indication";  
            tvProtocols.ParentTableRelation.Add(rf);  
 
            grid.MasterTableView.DetailTables.Add(tvProtocols);  
 
            divgrid.Controls.Add(grid);  
        }  
 
        void grid_NeedIndications(object source, GridNeedDataSourceEventArgs e)  
        {  
            OdbcCommand command1 = new OdbcCommand("select indication from indications order by indication asc;", connection);  
            OdbcDataReader reader1 = command1.ExecuteReader();  
            RadGrid grid = (RadGrid)divgrid.FindControl("grid1");  
            grid.DataSource = reader1;  
 
            OdbcCommand command2 = new OdbcCommand("select * from protocols;", connection);  
            OdbcDataReader reader2 = command2.ExecuteReader();  
            grid.MasterTableView.DetailTables[0].DataSource = reader2;  
        }  
 
    }  
}  
 

I have done a lot of searching on this forum but haven't found the answer yet.

It would be really nice if examples included more instances of programmatic creation of data sources rather than relying almost exclusively on declarative data sources, which I never use.

Thanks

2 Answers, 1 is accepted

Sort by
0
David Russell
Top achievements
Rank 1
answered on 22 May 2010, 11:08 PM
I have spent hours on this with no success.

By the way, when I reimplemented the building of the grid to happen in PageLoad, I followed your code exactly and it crashed with an index out of range exception when the grid was added to the placeholder.

I'm not doing so well today.
0
Veli
Telerik team
answered on 27 May 2010, 12:01 PM
Hello David,

The databinding of the detail tables is not setup properly. You may not be getting the correct data. The ParentTableRelations that  you are setting are intended for usage with declarative data source controls only. For programmatic grid definition and advanced databinding using NeedDataSource event, you need to set up your detail table binding using the DetailTableDataBind event.

Effectively, you need to remove the ParentTableRelation definition and register for RadGrid's DetailTableDataBind event. Inside the event handler, you need to get the data key of the parent item:

void grid_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)
{
    string indication = (string)e.DetailTableView.ParentItem.GetDataKeyValue("indication");
}

You then need to pass the table data using e.DetailTableView.DataSource and the data should contain only the items for  the specified indication value.

Sincerely yours,
Veli
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
David Russell
Top achievements
Rank 1
Answers by
David Russell
Top achievements
Rank 1
Veli
Telerik team
Share this question
or