using
System;
using
Telerik.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data;
using
System.Data.SqlClient;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
namespace
Telerik.GridExamplesCSharp.Programming.Hierarchy
{
public partial class OneView : System.Web.UI.Page
{
private void DefineGridStructure()
{
RadGrid RadGrid1 = new RadGrid();
RadGrid1.ID =
"RadGrid1";
//RadGrid1.DataSourceID = "SqlDataSource1";
RadGrid1.AutoGenerateColumns =
false;
RadGrid1.Skin =
"Web20";
RadGrid1.MasterTableView.DataKeyNames =
new string[] { "CustomerID" };
RadGrid1.Width =
Unit.Percentage(98);
RadGrid1.PageSize = 3;
RadGrid1.AllowPaging =
true;
RadGrid1.AllowSorting =
true;
RadGrid1.PagerStyle.Mode =
GridPagerMode.NextPrevAndNumeric;
RadGrid1.AutoGenerateColumns =
false;
RadGrid1.ShowStatusBar =
true;
RadGrid1.MasterTableView.PageSize = 3;
//Add columns
GridBoundColumn boundColumn;
boundColumn =
new GridBoundColumn();
boundColumn.DataField =
"CustomerID";
boundColumn.HeaderText =
"CustomerID";
RadGrid1.MasterTableView.Columns.Add(boundColumn);
boundColumn =
new GridBoundColumn();
boundColumn.DataField =
"ContactName";
boundColumn.HeaderText =
"Contact Name";
RadGrid1.MasterTableView.Columns.Add(boundColumn);
RadGrid1.DataSource = MakeDS().Tables[0];
RadGrid1.DataBind();
//Detail table - Orders (II in hierarchy level)
GridTableView tableViewOrders = new GridTableView(RadGrid1);
tableViewOrders.AutoGenerateColumns =
false;
//tableViewOrders.DataSourceID = "SqlDataSource2";
tableViewOrders.da
////Add columns
boundColumn =
new GridBoundColumn();
boundColumn.DataField =
"OrderID";
boundColumn.HeaderText =
"OrderID";
tableViewOrders.Columns.Add(boundColumn);
boundColumn =
new GridBoundColumn();
boundColumn.DataField =
"OrderDate";
boundColumn.HeaderText =
"Date Ordered";
tableViewOrders.Columns.Add(boundColumn);
tableViewOrders.DataSource = MakeDS().Tables[1];
tableViewOrders.DataBind();
tableViewOrders.Width =
Unit.Percentage(100);
tableViewOrders.DataKeyNames =
new string[] { "OrderID" };
GridRelationFields relationFields = new GridRelationFields();
relationFields.MasterKeyField =
"CustomerID";
relationFields.DetailKeyField =
"CustomerID";
tableViewOrders.ParentTableRelation.Add(relationFields);
RadGrid1.MasterTableView.DetailTables.Add(tableViewOrders);
//Add the RadGrid instance to the controls
this.PlaceHolder1.Controls.Add( RadGrid1 );
}
protected void Page_Init(object source, System.EventArgs e)
{
DefineGridStructure();
}
protected void Page_Load(object sender, EventArgs e)
{
RadGrid Grid1 = (RadGrid)PlaceHolder1.FindControl("RadGrid1");
}
public DataSet MakeDS()
{
SqlDataAdapter DA = new SqlDataAdapter();
DataSet DS = new DataSet();
SqlCommand cmd;
string strconnection =
@"Data Source=MY\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True";
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = strconnection;
cmd =
new SqlCommand("SELECT * FROM Customers", sqlcon);
cmd.Connection.Open();
DA.SelectCommand = cmd;
//creating datasets
DA.Fill(DS,
"MainTable");
cmd =
new SqlCommand("SELECT * FROM Orders ", sqlcon);
DA.SelectCommand = cmd;
DA.Fill(DS,
"SecondTable");
cmd.Connection.Close();
return DS;
}
}
}
Pls help me out!
Thanks for your help in advance.
<
asp:Panel id="pnlMenu" width="100%" runat="server" CssClass="collapsePanel">
<ak:MainMenu width="100%" height="100%" id="AppMainMenu" Runat="Server" />
</asp:Panel>
| protected void Page_Load(object sender, EventArgs e) { |
| if (!IsPostBack) { |
| RadTreeView1.DataSource = new NHibernateDaoFactory().GetTagGroupDao().GetAll(); |
| RadTreeView1.DataBind(); |
| RadTreeNode untaggedNode = new RadTreeNode(GetLocalResourceObject("res_untagged").ToString(), ""); |
| untaggedNode.AllowDrag = true; |
| untaggedNode.Checkable = true; |
| RadTreeView1.Nodes.Add(untaggedNode); |
| uxTreeAssessments.DataSource = new NHibernateDaoFactory().GetAssessmentDao().GetAll(); |
| uxTreeAssessments.DataFieldID = "ID"; |
| uxTreeAssessments.DataValueField = "ID"; |
| uxTreeAssessments.DataTextField = "LocalizedName"; |
| uxTreeAssessments.DataBind(); |
| } |
| } |
| protected void uxTreeAssessments_NodeDataBound(object sender, Telerik.Web.UI.RadTreeNodeEventArgs e) { |
| if (e.Node.Level == 0) { |
| Assessment a = e.Node.DataItem as Assessment; |
| foreach (Folder f in a.Folders) { |
| RadTreeNode n = new RadTreeNode( |
| string.Format("{0} ({1})", f.LocalizedName, f.Questions.Count), |
| f.ID.ToString()); |
| n.DataItem = f; |
| e.Node.Nodes.Add(n); |
| } |
| } else if (e.Node.Level > 0) { |
| Folder parent = e.Node.DataItem as Folder; |
| foreach (Folder f in parent.Folders) { |
| RadTreeNode n = new RadTreeNode( |
| string.Format("{0} ({1})", f.LocalizedName, f.Questions.Count), |
| f.ID.ToString()); |
| n.DataItem = f; |
| e.Node.Nodes.Add(n); |
| } |
| } |
| } |
| private void createGrid() | |
| { | |
| grdRuns.ID = "grdRuns"; | |
| grdRuns.ColumnCreated -= new GridColumnCreatedEventHandler(grdRuns_ColumnCreated); | |
| grdRuns.ItemCreated -= new GridItemEventHandler(grdRuns_ItemCreated); | |
| grdRuns.ItemDataBound -= new GridItemEventHandler(grdRuns_ItemDataBound); | |
| grdRuns.AutoGenerateColumns = false; | |
| grdRuns.HorizontalAlign = HorizontalAlign.Right; | |
| grdRuns.MasterTableView.HorizontalAlign = HorizontalAlign.Right; | |
| grdRuns.MasterTableView.Dir = GridTableTextDirection.RTL; | |
| grdRuns.EnableEmbeddedSkins = false; | |
| grdRuns.Skin = "Portal"; | |
| grdRuns.ClientSettings.Scrolling.UseStaticHeaders = true; | |
| grdRuns.ClientSettings.Scrolling.AllowScroll = true; | |
| grdRuns.MasterTableView.HierarchyDefaultExpanded = true; | |
| grdRuns.MasterTableView.TableLayout = GridTableLayout.Fixed; | |
| grdRuns.AutoGenerateEditColumn = true; | |
| grdRuns.AllowAutomaticUpdates = true; | |
| grdRuns.MasterTableView.EditMode = GridEditMode.EditForms; | |
| grdRuns.ColumnCreated += new GridColumnCreatedEventHandler(grdRuns_ColumnCreated); | |
| grdRuns.ItemCreated += new GridItemEventHandler(grdRuns_ItemCreated); | |
| grdRuns.ItemDataBound += new GridItemEventHandler(grdRuns_ItemDataBound); | |
| NostroDataSet.FieldsForGridDataTable dtFields = BLManager.GetFieldsForGrid(int.Parse(ddlRuns.SelectedValue)); | |
| foreach (NostroDataSet.FieldsForGridRow row in dtFields.Rows) | |
| { | |
| if (row.IsEditable) | |
| { | |
| GridTemplateColumn templateColumn = new GridTemplateColumn(); | |
| string templateColumnName = row.FieldName; | |
| templateColumn.ItemTemplate = new MyTemplate(templateColumnName); | |
| templateColumn.EditItemTemplate = new MyEditTemplate(templateColumnName); | |
| templateColumn.HeaderText = row.ScreenName; | |
| templateColumn.Display = row.Display; | |
| templateColumn.HeaderStyle.Width = Unit.Pixel(row.Witdh); | |
| templateColumn.ItemStyle.Width = Unit.Pixel(row.Witdh); | |
| grdRuns.MasterTableView.Columns.Add(templateColumn); | |
| } | |
| else | |
| { | |
| GridBoundColumn clm = new GridBoundColumn(); | |
| clm.DataField = row.FieldName; | |
| clm.HeaderText = row.ScreenName; | |
| clm.UniqueName = row.FieldName; | |
| clm.HeaderStyle.Wrap = false; | |
| clm.ItemStyle.Wrap = false; | |
| clm.HeaderStyle.Width = Unit.Pixel(row.Witdh); | |
| clm.ItemStyle.Width = Unit.Pixel(row.Witdh); | |
| grdRuns.MasterTableView.Columns.Add(clm); | |
| clm.Display = row.Display; | |
| } | |
| } | |
| grdRuns.ClientSettings.AllowExpandCollapse = true; | |
| grdRuns.MasterTableView.HierarchyLoadMode = GridChildLoadMode.Client; | |
| grdRuns.MasterTableView.HierarchyDefaultExpanded = false; | |
| grdRuns.MasterTableView.DataKeyNames = new string[] { "PortfolioID", "ParentID" }; | |
| grdRuns.MasterTableView.SelfHierarchySettings.ParentKeyName = "ParentID"; | |
| grdRuns.MasterTableView.SelfHierarchySettings.KeyName = "PortfolioID"; | |
| grdRuns.EditCommand += new GridCommandEventHandler(grdRuns_EditCommand); | |
| divGrid.Controls.Add(grdRuns); | |
| grdRuns.DataSource = ApproveRunData; | |
| } |
| private class MyTemplate : ITemplate | |
| { | |
| //protected LiteralControl lControl; | |
| protected Label label; | |
| private string colname; | |
| public MyTemplate(string cName) | |
| { | |
| colname = cName; | |
| } | |
| public void InstantiateIn(System.Web.UI.Control container) | |
| { | |
| //lControl = new LiteralControl(); | |
| //lControl.ID = "lControl"; | |
| //lControl.DataBinding += new EventHandler(lControl_DataBinding); | |
| label = new Label(); | |
| label.ID = "templateColumnLabel"; | |
| label.DataBinding += new EventHandler(label_DataBinding); | |
| container.Controls.Add(label); | |
| } | |
| void label_DataBinding(object sender, EventArgs e) | |
| { | |
| Label lbl = (Label)sender; | |
| GridDataItem container = (GridDataItem)lbl.NamingContainer; | |
| string sValue = ((DataRowView)container.DataItem)[colname].ToString(); | |
| double dValue = -1; | |
| if (double.TryParse(sValue, out dValue)) | |
| { | |
| string sTemp = String.Format("{0:n}", dValue); //add commas as thousand separator | |
| lbl.Text = sTemp.Substring(0, sTemp.IndexOf('.')); //don't display digits after decimal point. | |
| lbl.Style.Add("text-align", "left"); //make the minus sign ('-') appear on the left of the number. | |
| } | |
| else lbl.Text = sValue; | |
| if (sValue == "") lbl.Text = " "; | |
| } | |
| public void lControl_DataBinding(object sender, EventArgs e) | |
| { | |
| LiteralControl l = (LiteralControl)sender; | |
| GridDataItem container = (GridDataItem)l.NamingContainer; | |
| string sValue = ((DataRowView)container.DataItem)[colname].ToString(); | |
| double dValue = -1; | |
| if (double.TryParse(sValue, out dValue)) | |
| { | |
| string sTemp = String.Format("{0:n}", dValue); //add commas as thousand separator | |
| l.Text = sTemp.Substring(0, sTemp.IndexOf('.')); //don't display digits after decimal point. | |
| //l.Style.Add("text-align", "left"); //make the minus sign ('-') appear on the left of the number. | |
| } | |
| else l.Text = sValue + "<br/>"; | |
| if (sValue == "") l.Text = " "; | |
| //l.Text = ((DataRowView)container.DataItem)[colname].ToString() + "<br />"; | |
| } | |
| } | |
| private class MyEditTemplate : ITemplate | |
| { | |
| protected LiteralControl lControl; | |
| protected TextBox textBox; | |
| private string colname; | |
| public MyEditTemplate(string cName) | |
| { | |
| colname = cName; | |
| } | |
| public void InstantiateIn(System.Web.UI.Control container) | |
| { | |
| lControl = new LiteralControl(); | |
| lControl.ID = "lControl"; | |
| lControl.DataBinding += new EventHandler(lControl_DataBinding); | |
| textBox = new TextBox(); | |
| textBox.ID = "templateColumnTextBox"; | |
| textBox.CssClass = "fieldUpdatePortal"; | |
| textBox.DataBinding += new EventHandler(textBox_DataBinding); | |
| container.Controls.Add(textBox); | |
| } | |
| void textBox_DataBinding(object sender, EventArgs e) | |
| { | |
| TextBox tBox = (TextBox)sender; | |
| GridDataItem container = (GridDataItem)tBox.NamingContainer; | |
| string sValue = ((DataRowView)container.DataItem)[colname].ToString(); | |
| double dValue = -1; | |
| if (double.TryParse(sValue, out dValue)) | |
| { | |
| string sTemp = String.Format("{0:n}", dValue); //add commas as thousand separator | |
| tBox.Text = sTemp.Substring(0, sTemp.IndexOf('.')); //don't display digits after decimal point. | |
| tBox.Style.Add("direction", "ltr"); //make the minus sign ('-') appear on the left of the number. | |
| } | |
| else tBox.Text = sValue; | |
| } | |
| public void lControl_DataBinding(object sender, EventArgs e) | |
| { | |
| LiteralControl l = (LiteralControl)sender; | |
| GridDataItem container = (GridDataItem)l.NamingContainer; | |
| l.Text = ((DataRowView)container.DataItem)[colname].ToString() + "<br />"; | |
| } | |
| } | |
I need to show list of questions in grid and their answers in details view when a person expnads question item. Below is how I set datasource of radGrid. How can i set the datasource of nested details grid now?
protected void ShowQuestions()
{
try
{
this.radGridQuestions.DataSource = this.questions;
}
catch
{
}
}
<
tk:RadGrid ID="radGridQuestions" runat="server" AutoGenerateColumns="False" ShowHeader="false" GridLines="None">
<MasterTableView DataKeyNames="Id">
<Columns>
<tk:GridTemplateColumn>
<ItemTemplate>
<table>
<tr>
<td style="width: 30px; vertical-align: top;">
<img src="<%# PostStatusImage(Container.DataItem)%>" alt="PostStatus" />
</td>
<td style="vertical-align: top;">
<asp:LinkButton ID="lbViewQuestion" runat="server" Text='<%# Eval("Title") %>' CommandArgument='<%# Eval("Id") %>'
CommandName="ViewQuestion"></asp:LinkButton> Posted On: <%# Eval("PostedDate") %>
</td>
</tr>
</table>
</ItemTemplate>
</tk:GridTemplateColumn>
</Columns>
<DetailTables>
<tk:GridTableView runat="server" DataKeyNames="Id">
<ParentTableRelation>
<tk:GridRelationFields DetailKeyField="ParentThreadId" MasterKeyField="Id" />
</ParentTableRelation>
<DetailTables>
</DetailTables>
</tk:GridTableView>
</DetailTables>
</MasterTableView>
</tk:RadGrid>
