| <telerik:RadGrid ID="RadGrid1" AllowSorting="true" AllowPaging="true" AllowFilteringByColumn="true" |
| AutoGenerateColumns="false" runat="server"> |
| <MasterTableView > |
| <Columns> |
| <telerik:GridBoundColumn DataField="DocID" HeaderText="DocID" /> |
| <telerik:GridBoundColumn DataField="Title" HeaderText="Title" /> |
| </Columns> |
| </MasterTableView> |
| <PagerStyle Mode="NumericPages" /> |
| <ClientSettings> |
| <DataBinding Location="http://localhost/iProWS/GetGridData.asmx" |
| SelectMethod="GetDataAndCount" EnableCaching="true" /> |
| <ClientEvents OnDataBinding="RadGrid1_DataBinding" /> |
| </ClientSettings> |
| </telerik:RadGrid> |
| function RadGrid1_DataBinding(sender, args) |
| { |
| var methodArguments = args.get_methodArguments(); |
| methodArguments.WhereCond = "myValue"; |
| args.set_methodArguments(methodArguments); |
| } |
| function RadGrid1_DataBinding(sender, args) |
| { |
| var myMethodArguments = new Object(); |
| myMethodArguments.WhereCond = "myValue"; |
| args.set_methodArguments(myMethodArguments); |
| } |
| <WebMethod(EnableSession:=True)> _ |
| Public Function GetDataAndCount(ByVal startRowIndex As Integer, ByVal maximumRows As Integer, ByVal sortExpression As List(Of GridSortExpression), ByVal filterExpression As List(Of GridFilterExpression), ByVal WhereCond As String) As Dictionary(Of String, Object) |
| Dim data As New Dictionary(Of String, Object)() |
| data.Add("Data", GetData(startRowIndex, maximumRows, sortExpression, filterExpression, WhereCond)) |
| data.Add("Count", DirectCast(Session("Count"), Integer)) |
| Return data |
| End Function |
I want to export RadGrid styles to PDF. I have a radgrid in a normal .aspx page, and I use a usercontrol to export the radgrid to pdf format. I have read the tips and tricks article, but am still unable to get exporting styles to PDF to work.
Here is the radgrid in a normal aspx page:
| <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> |
| <%@ Register TagPrefix="uc1" TagName="Export" Src="~/Controls/ctrlExportReport.ascx" %> |
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| <html xmlns="http://www.w3.org/1999/xhtml"> |
| <head runat="server"> |
| <title></title> |
| </head> |
| <body> |
| <form id="form1" runat="server"> |
| <div> |
| <asp:ScriptManager ID="sm1" runat="server"></asp:ScriptManager> |
| <table style="width: 100%"> |
| <tr> |
| <td> |
| <telerik:radwindowmanager id="rwm" runat="server" modal="true" reloadonshow="true" visibletitlebar="True" behavior="close, move" visiblestatusbar="false"> |
| </telerik:radwindowmanager> |
| <telerik:RadAjaxLoadingPanel id="rdpInterventions" runat="server" cssclass="DataGrid" style="text-align: center; background-color: White; width: 100%"> |
| <p class="header"> |
| Loading interventions due within the selected date range</p> |
| <br /> |
| <p> |
| <img id="Img1" alt="loading..." runat="server" src="~/Images/LoadingProgressBar.gif" /></p> |
| </telerik:RadAjaxLoadingPanel> |
| <telerik:radajaxpanel id="rap" runat="server" enableajax="true" LoadingPanelID="rdpInterventions"> |
| <Telerik:radgrid id="rg" runat="server" autogeneratecolumns="true" width="100%" Skin="" > |
| <mastertableview allowsorting="true" cssclass="DataGrid" gridlines="horizontal" showfooter="true" width="100%"> |
| <HeaderStyle forecolor="blue" BackColor="Black" /> |
| <DetailTables> |
| <telerik:GridTableView DataMember="Detail" DataKeyNames="SourceID" Width="95%"> |
| <ParentTableRelation> |
| <telerik:GridRelationFields MasterKeyField="SourceID" DetailKeyField="SourceID" /> |
| </ParentTableRelation> |
| </telerik:GridTableView> |
| </DetailTables> |
| </MasterTableView> |
| </telerik:RadGrid> |
| </telerik:radajaxpanel> |
| </td> |
| </tr> |
| <tr> |
| <td style="height: 25px"> |
| <uc1:Export ID="ctrlExport1" runat="server" ControlName="rg" /> |
| </td> |
| </tr> |
| </table> |
| </div> |
| </form> |
| </body> |
| </html> |
| <%@ Control Language="VB" AutoEventWireup="false" CodeFile="ctrlExportReport.ascx.vb" Inherits="ctrlExportReport" %> |
| <asp:ImageButton ID="btnExportToExcel" runat="server" AlternateText="Export to Excel" ImageUrl="~/Images/Export2Excel.gif" ToolTip="Export to Excel" /> |
| |
| <asp:ImageButton ID="btnExportToPDF" runat="server" AlternateText="Export to PDF" ImageUrl="~/Images/Export2Pdf.gif" ToolTip="Export to PDF" /> |
| <input id="hdnHidePDF" runat="Server" type="hidden" /> |
| ''' <summary> |
| ''' Export a RadGrid to either Excel or PDF. |
| ''' </summary> |
| Partial Class ctrlExportReport |
| Inherits System.Web.UI.UserControl |
| #Region " Declarations " |
| 'The ID of the RadGrid displaying the report. |
| Private strControlName As String = String.Empty |
| #End Region 'Declarations |
| #Region " Properties " |
| 'The ID of the RadGrid displaying the report. |
| Public Property ControlName() As String |
| Get |
| If String.IsNullOrEmpty(strControlName) = False Then |
| Return strControlName |
| End If |
| Return String.Empty |
| End Get |
| Set(ByVal value As String) |
| strControlName = value |
| End Set |
| End Property |
| 'Boolean whether or not to display the PDF button. |
| Public Property HidePDF() As Boolean |
| Get |
| If String.IsNullOrEmpty(hdnHidePDF.Value) Then |
| Return False |
| Else |
| Return hdnHidePDF.Value |
| End If |
| End Get |
| Set(ByVal value As Boolean) |
| hdnHidePDF.Value = value |
| End Set |
| End Property |
| 'Boolean whether or not to display the page title. |
| Public Property IncludePageTitle() As Boolean |
| Get |
| If ViewState.Item("IncludePageTitle") Is Nothing Then |
| Return False |
| Else |
| Return ViewState.Item("IncludePageTitle") |
| End If |
| End Get |
| Set(ByVal value As Boolean) |
| ViewState.Item("IncludePageTitle") = value |
| End Set |
| End Property |
| 'The BaseFileName constant with the current date appended. |
| Private ReadOnly Property FileName() As String |
| Get |
| Return String.Format("{0}-{1}-{2}", Now.Year.ToString(), PadDatePart(Now.Month), PadDatePart(Now.Day)).Insert(0, AppFileName) |
| End Get |
| End Property |
| Public Property PageTitle() As String |
| Get |
| If ViewState.Item("PageTitle") Is Nothing Then |
| Return ConfigurationManager.AppSettings("DefaultPageTitle") |
| Else |
| Return ViewState.Item("PageTitle").ToString() |
| End If |
| End Get |
| Set(ByVal value As String) |
| ViewState.Item("PageTitle") = value |
| End Set |
| End Property |
| Private ReadOnly Property AppFileName() As String |
| Get |
| If Not String.IsNullOrEmpty(ConfigurationManager.AppSettings("AppFileName")) Then |
| Return ConfigurationManager.AppSettings("AppFileName").ToString |
| Else |
| Return "GIC" |
| End If |
| End Get |
| End Property |
| Private ReadOnly Property Report() As RadGrid |
| Get |
| If String.IsNullOrEmpty(Me.ControlName) = False Then |
| Dim objControl As Control = Parent.FindControl(Me.ControlName) |
| If objControl IsNot Nothing Then |
| Try |
| Return DirectCast(objControl, RadGrid) |
| Catch ex As Exception |
| 'objControl could not be cast to a RadGrid. Free |
| 'resources and return Nothing. |
| objControl = Nothing |
| End Try |
| End If |
| End If |
| Return Nothing |
| End Get |
| End Property |
| #End Region 'Properties |
| #Region " Events " |
| Protected Sub btnExportToExcel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnExportToExcel.Click |
| If Me.Report IsNot Nothing Then |
| Me.ConfigureExportSettings() |
| Report.MasterTableView.ExportToExcel() |
| End If |
| End Sub |
| Protected Sub btnExportToPDF_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnExportToPDF.Click |
| If Me.Report IsNot Nothing Then |
| '-- 1) Expand and rebind the grid -- |
| Me.Report.MasterTableView.HierarchyDefaultExpanded = True |
| Me.Report.Rebind() |
| '-- 2) Setup the export settings -- |
| Me.ConfigureExportSettings(True) |
| '-- 3) Export the report -- |
| With Report |
| .Page.Response.ClearHeaders() |
| .Page.Response.Cache.SetCacheability(HttpCacheability.Private) |
| .MasterTableView.ExportToPdf() |
| End With |
| End If |
| End Sub |
| ' Check to see if the HidePDF property has been set - if so, hide that button. |
| Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender |
| If HidePDF Then |
| Me.btnExportToPDF.Visible = False |
| End If |
| End Sub |
| #End Region 'Events |
| #Region " Methods " |
| 'These settings are shared by both Excel and PDF. |
| Private Sub ConfigureExportSettings(Optional ByVal bPdf As Boolean = False) |
| '-- 1) Set the global report export settings -- |
| With Report.ExportSettings |
| .OpenInNewWindow = True 'Makes GUI cleaner. |
| .FileName = Me.FileName |
| .ExportOnlyData = True 'Header will be included. |
| .IgnorePaging = True 'All Records - setting this to true means if the grid is collapsed by default, the PDF will not show |
| ' the detail items even if the user expands the parent items manually. Conversely, if the grid is expanded by default, the |
| ' pdf will show the detail items even if the user collapses them. |
| End With |
| '-- 2) Set the msater table font and styles -- |
| With Report.MasterTableView.Font |
| .Name = "Arial" |
| .Size = New FontUnit(9, UnitType.Point) |
| End With |
| With Report.MasterTableView.HeaderStyle.Font |
| .Name = "Arial" |
| .Size = New FontUnit(9, UnitType.Point) |
| End With |
| '-- 3) Hide any of the command items (if applicable) -- |
| For Each commandItem As GridItem In Report.MasterTableView.GetItems(GridItemType.CommandItem) |
| commandItem.Visible = False |
| Next |
| '-- 4) Set the Pdf Export Settings if doing the Pdf export -- |
| If bPdf Then |
| With Report.ExportSettings.Pdf |
| If IncludePageTitle Then |
| .PageTitle = PageTitle |
| Else |
| .PageTitle = "" |
| End If |
| .PageTopMargin = New Unit(0.7, UnitType.Inch) |
| .FontType = Telerik.Web.Apoc.Render.Pdf.FontType.Embed |
| .PageBottomMargin = New Unit(0.4, UnitType.Inch) |
| .PageLeftMargin = .PageBottomMargin |
| .PageRightMargin = .PageBottomMargin |
| End With |
| Dim headerItem As GridHeaderItem |
| For Each headerItem In Report.MasterTableView.GetItems(GridItemType.Header) |
| headerItem.Style("font-size") = "16pt" |
| headerItem.Style("color") = "red" |
| headerItem.Style("background-color") = "yellow" |
| headerItem.Style("height") = "50px" |
| headerItem.Style("vertical-align") = "bottom" |
| For Each cell As TableCell In headerItem.Cells |
| cell.Style("text-align") = "left" |
| cell.Style("font-weight") = "bold" |
| cell.Style("border-color") = "red" |
| Next |
| Next |
| End If |
| End Sub |
| Private Function PadDatePart( _ |
| ByVal intDatePart As Integer) As String |
| Dim strDatePart As String = intDatePart.ToString() |
| If strDatePart.Length = 1 Then |
| strDatePart = "0" & strDatePart |
| End If |
| Return strDatePart |
| End Function |
| #End Region 'Methods |
| End Class 'Partial "ctrlExportReport" |
| <telerik:RadMenuItem Text="Defendant" onclick="return Redirect(this.href);" NavigateUrl="Parties/Defendant.aspx"> |
| </telerik:RadMenuItem> |
| <script language="javascript" type="text/javascript"> |
| function Redirect( url) |
| { |
| document.getElementById("Frame").src= url; |
| return false; |
| } |
| </script> |
Hi
I'm using Q3 2008 release
I have been trying to emulate the hierarchy with template demo. I am using the Northwind database. My master table contains customers in the database.
I have been able to create a NestedViewTemplate that contains a hidden label containing the primary key for the parent row. I then use this row as a control parameter in an sql data source to populate a radgrid nested inside the NestedViewTemplate with orders for the customer ( i know i could use a standard master detail table for this but because of a layout demands I need to investigate this NestedViewTemplate). I have been able to populate the nested radgrid declaratively but I would like to know how to populate it programmatically. I have played around in the detail table data bind event to try and find some way to access the controls in the NestedViewTemplate but to no avail.
I have pasted the code below for the declarative 'version' which works ok
Many thanks in advace
<
asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<telerik:RadGrid ID="RadGrid1" runat="server" >
<MasterTableView DataKeyNames="CustomerID" >
<ExpandCollapseColumn Visible="True">
</ExpandCollapseColumn>
<NestedViewTemplate>
<telerik:RadTabStrip runat="server" ID="TabStrip1" MultiPageID="Multipage1"
SelectedIndex="0">
<Tabs>
<telerik:RadTab runat="server" Text="Orders" PageViewID="PageView1">
</telerik:RadTab>
<telerik:RadTab runat="server" Text="Contact Information" PageViewID="PageView2">
</telerik:RadTab>
<telerik:RadTab runat="server" Text="Statistics Chart" PageViewID="PageView3">
</telerik:RadTab>
</Tabs>
</telerik:RadTabStrip>
<telerik:RadMultiPage runat="server" ID="Multipage1" RenderSelectedPageOnly="false">
<telerik:RadPageView runat="server" ID="PageView1">
<asp:Label ID="Label1" Font-Bold="true" Font-Italic="true" Text='<%# Eval("CustomerID") %>'
runat="server" />
PageView 1
<telerik:RadGrid runat="server" ID="RadGrid2">
<MasterTableView DataSourceID="SqlDataSource2">
<Columns>
<telerik:GridBoundColumn DataField="OrderDate"></telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource2" ConnectionString="Data Source=OSCAR\SQLExpress;Initial Catalog=Northwind;Integrated Security=true;"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM Orders Where CustomerID = @CustomerID"
runat="server">
<SelectParameters>
<asp:ControlParameter ControlID="Label1" PropertyName="Text" Type="String" Name="CustomerID" />
</SelectParameters>
</asp:SqlDataSource>
</telerik:RadPageView>
</telerik:RadMultiPage>
</NestedViewTemplate>
</MasterTableView>
</telerik:RadGrid>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="Rad" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Label ID="l" runat="server" />
<Rad:RadMenu ID="TabMenu" runat="server" OnItemClick="TabMenu_ItemClick" style="position:absolute;left:0px;top:103px;z-index:0;">
<Items>
<Rad:RadMenuItem Text="Choice 1">
</Rad:RadMenuItem>
<Rad:RadMenuItem Text="Choice 2">
</Rad:RadMenuItem>
</Items>
</Rad:RadMenu>
<Rad:RadGrid ID="RadGrid1" runat="server" Skin="Default" Gridlines="None" AllowSorting="True" EnableLinqExpressions="False"
AutoGenerateColumns="True" style="position:absolute;top:129px;left:0px;" OnItemCreated="RG1_ItemCreated">
<PagerStyle Mode="NextPrevAndNumeric" />
<MasterTableView AutoGenerateColumns="True" TableLayout="Fixed" />
<ClientSettings ReorderColumnsOnClient="True" AllowColumnsReorder="True">
<Scrolling AllowScroll="True" UseStaticHeaders="True" SaveScrollPosition="True" />
</ClientSettings>
</Rad:RadGrid>
<asp:SqlDataSource ID="SqlDS1" runat="server"
ConnectionString=..........................."
SelectCommand="SELECT * FROM [Table1]" />
<asp:SqlDataSource ID="SqlDS2" runat="server"
ConnectionString=".........................."
SelectCommand="SELECT * FROM [Table2]" />
</form>
</body>
</html>
Code Behind
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
public partial class _Default : System.Web.UI.Page
{
static string tester;
static RadGrid TestGrid;
static string expression;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TabMenu_ItemClick(object sender, RadMenuEventArgs e)
{
l.Text = expression;
switch (e.Item.Text)
{
case "Choice 1":
RadGrid1.DataSourceID = "SqlDS1";
break;
case "Choice 2":
RadGrid1.DataSourceID = "SqlDS2";
break;
}
}
protected void RG1_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridHeaderItem)
{
foreach (GridColumn column in e.Item.OwnerTableView.RenderColumns)
{
if (!column.Visible)
continue;
((GridHeaderItem)e.Item)[column.UniqueName].Controls.Clear();
Label GridHeadLbl1 = new Label();
GridHeadLbl1.Text = column.HeaderText;
GridHeadLbl1.Style["float"] = "left";
((GridHeaderItem)e.Item)[column.UniqueName].Controls.Add(GridHeadLbl1);
RadMenu RadMenu1 = new RadMenu();
RadMenu1.Style["border"] = "0px";
RadMenu1.Style["float"] = "right";
RadMenu1.Style["margin-right"] = "10px";
RadMenu1.Items.Add(new RadMenuItem(""));
RadMenu1.Items[0].PostBack = false;
RadMenuItem MenuItem1 = new RadMenuItem("Sort Asc");
MenuItem1.Value = column.UniqueName;
RadMenu1.Items[0].Items.Add(MenuItem1);
RadMenuItem MenuItem2 = new RadMenuItem("Sort Des");
MenuItem1.Value = column.UniqueName;
RadMenu1.Items[0].Items.Add(MenuItem2);
RadMenuItem MenuItem3 = new RadMenuItem("Filter");
MenuItem1.Value = column.UniqueName;
RadMenu1.Items[0].Items.Add(MenuItem3);
RadMenuItem MenuSubItem1 = new RadMenuItem();
tester = column.UniqueName;
TestGrid = RadGrid1;
MenuSubItem1.ItemTemplate = new MyTemplate();
MenuSubItem1.Width = Unit.Pixel(150);
MenuSubItem1.Value = column.UniqueName;
MenuItem3.Items.Add(MenuSubItem1);
((GridHeaderItem)e.Item)[column.UniqueName].Controls.Add(RadMenu1);
}
}
}
public class MyTemplate : ITemplate
{
#region Itemplate Members
RadComboBox Combo1;
public void InstantiateIn(Control container)
{
Combo1 = new RadComboBox();
Combo1.AutoPostBack = true;
Combo1.AppendDataBoundItems = true;
Combo1.DataSourceID = TestGrid.DataSourceID;
Combo1.DataTextField = tester;
Combo1.DataValueField = tester;
Combo1.ItemDataBound += new RadComboBoxItemEventHandler(Combo1_ItemDataBound);
Combo1.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(Combo1_SelectedIndexChanged);
// Combo1.SelectedValue = RadGrid1.OwnerTableView.GetColumn(tester).CurrentFilterValue;
container.Controls.Add(Combo1);
}
public void Combo1_SelectedIndexChanged(object sender, EventArgs e)
{
RadMenuItem item = ((RadComboBox)sender).Parent as RadMenuItem;
expression = "";
RadGrid RadGrid1 = (RadGrid)item.Page.FindControl("RadGrid1");
GridColumn column = RadGrid1.MasterTableView.GetColumnSafe(item.Value);
if (column.DataType == typeof(string))
{
expression = String.Format(@"{0}.Contains(""{1}"")", item.Value, ((RadComboBox)sender).SelectedValue);
}
else
{
expression = String.Format("{0} == {1}", item.Value, ((RadComboBox)sender).SelectedValue);
}
RadGrid1.MasterTableView.FilterExpression = expression;
RadGrid1.Rebind();
}
public void Combo1_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
{
if (Combo1.FindItemByText(e.Item.Text).ClientID != e.Item.ClientID)
{
e.Item.Visible = false;
}
}
#endregion
}
}
I am trying to leverage the extensive editing capability built into RadGrid to replace a GridView / custom pages (insert/update code).
My data is being sourced from multiple tables and I am using LINQ to retrieve the data.
There is a small snippet of code below, all joined an an "OrgID" field. The join works fine and the resulting data is in fact what I am looking for. However, when I apply the data source and bind it, RadGrid cannot seem to display the columns, auto-generated or otherwise.
It might be related to how I am concanting the data (select new {p,o}) and if that is the case, I need to know what the prefered format of the data is.
My goal is to create a system using rad grid where I can edit a record and have it update as many as 10 tables at once that are all joined together without having to write any update/insert code (or very little).
| var v = (from p in m_db.OrgLists |
| join o in m_db.HostLocations on p.id equals o.OrgNameID |
| select new {p, o}); |
| // Get the data |
| rgOrgList.DataSource = v; |
| rgOrgList.DataBind(); |