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