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

adding control to groupheader

18 Answers 417 Views
Grid
This is a migrated thread and some comments may be shown as answers.
debbie f
Top achievements
Rank 1
debbie f asked on 30 Oct 2008, 08:32 PM
I have seen several posts on this topic, but have not seen a solution that will work yet.  I want to add a linkbutton to my group header that will use data from the group header in the postback.

I have something like the following:

LinkButton lnk = new LinkButton();
lnk.ID = "lnkReservations";
lnk.Text = "View All";
lnk.CommandArgument = (DataRowView)item.DataItem["employer_id"].ToString();
lnk.Command += new CommandEventHandler(lnkReservations_Command);
item.DataCell.Controls.Add(lnk);

I have tried adding to the event handlers for PreRender, ItemCreated, ItemDataBound but have not been successful in any of those attempts in getting the linkbutton to behave correctly.

Is there a simple solution to add a linkbutton to a groupheader that will execute a function based on the data/header where the control sits?  Is there any kind of special column that I can add to turn a header into a linkbutton?

18 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 31 Oct 2008, 05:39 AM
Hi Debbie,

Try adding the linkbutton in both ItemDataBound event and ItemCreated Event as shown below.

CS:
  protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridGroupHeaderItem) 
        { 
            GridGroupHeaderItem item = e.Item as GridGroupHeaderItem; 
            LinkButton lnk = new LinkButton(); 
            lnk.ID = "lnkReservations"
            lnk.Text = "View All"
            lnk.Command += new CommandEventHandler(lnkReservations_Command); 
            item.DataCell.Controls.Add(lnk); 
           
        } 
 
 
    } 
     
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
        if (e.Item is GridGroupHeaderItem) 
        { 
            GridGroupHeaderItem item = e.Item as GridGroupHeaderItem; 
            LinkButton lnk = new LinkButton(); 
            lnk.ID = "lnkReservations"
            lnk.Text = "View All"
            item.DataCell.Controls.Add(lnk); 
        } 
 
         
    } 
 
    void lnkReservations_Command(object sender, CommandEventArgs e) 
    { 
        LinkButton lnkReservations = (LinkButton)sender; 
        GridGroupHeaderItem groupHeader = (GridGroupHeaderItem)lnkReservations.NamingContainer; 
    } 


Thanks
Shinu.
0
debbie f
Top achievements
Rank 1
answered on 31 Oct 2008, 03:04 PM
I initially tried that sample from another forum post.  However, when the linkbutton's command is fired, groupHeader.DataItem is null. Is there anyway to actually access the data that I need to process the logic necessary for my linkbutton?  Even if there is a way to access the itemindex and pull from the datakeys, that would help.
0
Shinu
Top achievements
Rank 2
answered on 03 Nov 2008, 06:45 AM
Hi Debbie,

I hope you are trying to access the GroupByField from the GridGridGroupHeaderItem on clicking the linkbutton. If so try the following code snippet.

CS:
  void lnkReservations_Command(object sender, CommandEventArgs e) 
    { 
        LinkButton lnkReservations = (LinkButton)sender; 
        GridGroupHeaderItem groupHeader = (GridGroupHeaderItem)lnkReservations.NamingContainer; 
        string strtxt = groupHeader.DataCell.Text; 
        string[] arr = strtxt.Split(':'); 
        string strGroupByField = arr[0].ToString(); 
    }  


Thanks
Shinu.
0
Princy
Top achievements
Rank 2
answered on 03 Nov 2008, 09:23 AM
Hi,

A suggestion would be to split the  DataCell text of the group header and then loop through the GridDataItems to find the item with the particular data.

 LinkButton lnkReservations = (LinkButton)sender; 
            GridGroupHeaderItem groupHeader = (GridGroupHeaderItem)lnkReservations.NamingContainer; 
            string strText = groupHeader.DataCell.Text; 
            string[] arr = strText.Split(':'); 
            foreach (GridDataItem  item in  RadGrid1.Items) 
            { 
                if (arr[1].TrimStart() == item[arr[0].ToString()].Text) 
                { 
                    //get the data 
                } 
               
            } 


Thanks,
Princy
0
Zyguy
Top achievements
Rank 1
answered on 09 Feb 2009, 09:00 PM
Hi,

I found this example while searching for a way to achieve my goal:

I have a Radgrid grouped by Order ID's, with several items possible in each order group. I would like to make a button link in the header of each Order ID group, which will export an Invoice for the contents of the Order group.

I tried to translate this example into VB however get the error: "Expression of type 'Telerik.Web.UI.GridItem' can never be of type 'Telerik.WebControls.GridGroupHeaderItem'."

Can you please advise how I could adapt this example to achieve my goal or perhaps use another method such as the native Radgrid Export methods in order to export an HTML or PDF invoice for each Order ID group.     
0
Shinu
Top achievements
Rank 2
answered on 13 Feb 2009, 07:00 AM
Hi Zyguy,

I hope you are trying to export the Grouped data on clicking the Export button in the corresponding GridGroupHeader. Give a try with the following code snippet and see if it helps.

VB:
 
     Protected Sub RadGrid1_ItemCreated(ByVal sender As ObjectByVal e As GridItemEventArgs) 
         If TypeOf e.Item Is GridGroupHeaderItem Then 
             Dim item As GridGroupHeaderItem = TryCast(e.Item, GridGroupHeaderItem) 
             Dim lnk As New LinkButton() 
             lnk.ID = "lnkReservations" 
             lnk.Text = "ExportGroup" 
             AddHandler lnk.Command, AddressOf lnkReservations_Command 
             item.DataCell.Controls.Add(lnk) 
             
         End If 
         
     End Sub 
     
     Protected Sub RadGrid1_ItemDataBound(ByVal sender As ObjectByVal e As GridItemEventArgs) 
         
         
         If TypeOf e.Item Is GridGroupHeaderItem Then 
             Dim item As GridGroupHeaderItem = TryCast(e.Item, GridGroupHeaderItem) 
             Dim lnk As New LinkButton() 
             lnk.ID = "lnkReservations" 
             lnk.Text = "ExportGroup" 
             item.DataCell.Controls.Add(lnk) 
         End If 
         
         
     End Sub 
     Private Sub lnkReservations_Command(ByVal sender As ObjectByVal e As CommandEventArgs) 
         Dim lnkReservations As LinkButton = DirectCast(sender, LinkButton) 
         Dim currentHeaderitm As GridGroupHeaderItem = DirectCast(lnkReservations.NamingContainer, GridGroupHeaderItem) 
         For Each headerItem As GridGroupHeaderItem In RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader) 
             If headerItem.GroupIndex <> currentHeaderitm.GroupIndex Then 
                 Dim children As GridItem() = headerItem.GetChildItems() 
                 For Each child As GridItem In children 
                     Dim dataItem As GridDataItem = TryCast(child, GridDataItem) 
                     dataItem.Visible = False 
                 Next 
                 headerItem.Visible = False 
             End If 
         Next 
         RadGrid1.ExportSettings.OpenInNewWindow = True 
         RadGrid1.ExportSettings.ExportOnlyData = True 
         RadGrid1.MasterTableView.ExportToPdf() 
     End Sub 
 
 


Hope this helps..
Shinu







0
Christopher Taylor
Top achievements
Rank 1
answered on 14 Dec 2009, 03:12 AM
I'm using just text to adjust the group header.  I want to use a subobject (i.e Component.ComponentProperty ) but can't get this to work.

So in turn.  I'm attaching to the ItemCreated and ItemDataBound events as mentioned in this article.  However, I've noticed some undesireable behavior I'm not sure how to correct. 

When it first renders, the group header has the text I would like.  But any subsequent expand and collapse still uses the data in the <SelectGroup> section. 

What's the best way to do this?

Thanks,
Chris

-edit: Added code

public partial class ComponentList : System.Web.UI.Page 
    { 
 
        SiteManagerDataDataContext dataContext;  
        protected void Page_Load(object sender, EventArgs e) 
        { 
           
            ComponentsDataSource.ContextCreated += new EventHandler<LinqDataSourceStatusEventArgs>(ComponentsDataSource_ContextCreated); 
            RadGrid1.ItemCreated += new Telerik.Web.UI.GridItemEventHandler(RadGrid1_ItemCreated); 
            RadGrid1.ItemDataBound += new GridItemEventHandler(RadGrid1_ItemDataBound); 
                 
         
        } 
 
         
        void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
            if (e.Item is Telerik.Web.UI.GridGroupHeaderItem) 
            { 
                DataRowView drv = e.Item.DataItem as DataRowView; 
                int componentId = int.Parse(drv["ComponentId"].ToString()); 
                var row = dataContext.Components.FirstOrDefault(o => o.ComponentId == componentId); 
 
 
                GridGroupHeaderItem item = e.Item as GridGroupHeaderItem; 
                LiteralControl litText = new LiteralControl(row.ComponentName); 
 
                item.DataCell.Controls.Clear(); 
                item.DataCell.Controls.Add(litText); 
 
            } 
        } 
 
   
 
        void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e) 
        { 
            if (e.Item is Telerik.Web.UI.GridGroupHeaderItem) 
            { 
                DataRowView drv = e.Item.DataItem as DataRowView; 
                int componentId = int.Parse(drv["ComponentId"].ToString()); 
                var row = dataContext.Components.FirstOrDefault(o => o.ComponentId == componentId); 
 
 
                GridGroupHeaderItem item = e.Item as GridGroupHeaderItem; 
                LiteralControl litText = new LiteralControl(row.ComponentName); 
 
                item.DataCell.Controls.Clear(); 
                item.DataCell.Controls.Add(litText); 
 
            }   
        } 
 
         
     
        void ComponentsDataSource_ContextCreated(object sender, LinqDataSourceStatusEventArgs e) 
        { 
            dataContext = e.Result as SiteManagerDataDataContext; 
        } 
 

aspx code

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" 
    CodeBehind="ComponentList.aspx.cs" Inherits="DNNSiteManager.Components.ComponentList" %> 
 
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"
    <asp:ScriptManager runat="server"></asp:ScriptManager> 
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" DataSourceID="ComponentsDataSource" 
        GridLines="None"
        <MasterTableView DataKeyNames="ComponentVersionId" DataSourceID="ComponentsDataSource"
            <RowIndicatorColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
            </RowIndicatorColumn> 
            <GroupByExpressions> 
               <telerik:GridGroupByExpression > 
                    <SelectFields> 
                        <telerik:GridGroupByField FieldAlias="ComponentId" FieldName="ComponentId" FormatString="" HeaderValueSeparator="-" /> 
                    </SelectFields> 
                    <GroupByFields> 
                        <telerik:GridGroupByField FieldAlias="ComponentId" FieldName="ComponentId" SortOrder="Descending"></telerik:GridGroupByField> 
                    </GroupByFields> 
                </telerik:GridGroupByExpression> 
            </GroupByExpressions> 
            <ExpandCollapseColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
            </ExpandCollapseColumn> 
            <Columns> 
        <telerik:GridBoundColumn DataField="ComponentVersionId" DataType="System.Int32" HeaderText="ComponentVersionId" 
                    ReadOnly="True" SortExpression="ComponentVersionId" UniqueName="ComponentVersionId">         
                </telerik:GridBoundColumn> 
        <telerik:GridBoundColumn DataField="Version" HeaderText="Version" SortExpression="Version" 
                    UniqueName="Version">         
                </telerik:GridBoundColumn> 
        <telerik:GridBoundColumn DataField="PackageUri" HeaderText="PackageUri" SortExpression="PackageUri" 
                    UniqueName="PackageUri">         
                </telerik:GridBoundColumn> 
        <telerik:GridBoundColumn DataField="ComponentId" DataType="System.Int32" HeaderText="ComponentId" 
                    SortExpression="ComponentId" UniqueName="ComponentId">         
                </telerik:GridBoundColumn> 
            </Columns> 
        </MasterTableView> 
    </telerik:RadGrid> 
    <asp:LinqDataSource runat="server" ID="ComponentsDataSource" ContextTypeName="DNNSiteManager.Data.SiteManagerDataDataContext" 
        EntityTypeName="" TableName="ComponentVersions"
    </asp:LinqDataSource> 
</asp:Content> 
 

0
Iana Tsolova
Telerik team
answered on 15 Dec 2009, 01:10 PM
Hi Christopher,

Could you please try modifying your code as below and see if this works for you:

protected void Page_Load(object sender, EventArgs e)  
{  
    ComponentsDataSource.ContextCreated += new EventHandler<LinqDataSourceStatusEventArgs>(ComponentsDataSource_ContextCreated);  
    RadGrid1.ItemDataBound += new GridItemEventHandler(RadGrid1_ItemDataBound);  
}  
void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
{  
    if (e.Item is Telerik.Web.UI.GridGroupHeaderItem)  
    {  
         DataRowView drv = e.Item.DataItem as DataRowView;  
         int componentId = int.Parse(drv["ComponentId"].ToString());  
         var row = dataContext.Components.FirstOrDefault(o => o.ComponentId == componentId);  
         GridGroupHeaderItem item = e.Item as GridGroupHeaderItem;  
         item.DataCell.Text = row.ComponentName;  
    }  
}

Regards,
Iana
the Telerik team


Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Kavya
Top achievements
Rank 2
answered on 18 Feb 2011, 11:43 PM
Hi princy
Please help me with this module

I know this is very old Thread. But i am trying to use your'e code . I also have

grid_PreRender function. Eventhough i add linkbutton in Itemcreated and Itemdatabound the link button is cleared in Prerender function.
I want to see my GroupName appended with the count of items in group header(reason to use Prerender).

Below is the way i am doing it


Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs)  
        If TypeOf e.Item Is GridGroupHeaderItem Then  
            Dim item As GridGroupHeaderItem = TryCast(e.Item, GridGroupHeaderItem)  
            Dim lnk As New LinkButton()  
            lnk.ID = "lnkReservations"  
            lnk.Text = "ExportGroup"  
            AddHandler lnk.Command, AddressOf lnkReservations_Command  
            item.DataCell.Controls.Add(lnk)  
               
        End If  
           
    End Sub  
       
    Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs)  
           
           
        If TypeOf e.Item Is GridGroupHeaderItem Then  
            Dim item As GridGroupHeaderItem = TryCast(e.Item, GridGroupHeaderItem)  
            Dim lnk As New LinkButton()  
            lnk.ID = "lnkReservations"  
            lnk.Text = "ExportGroup"  
            item.DataCell.Controls.Add(lnk)  
        End If  
           
           
    End Sub  
    Private Sub lnkReservations_Command(ByVal sender As Object, ByVal e As CommandEventArgs)  
        Dim lnkReservations As LinkButton = DirectCast(sender, LinkButton)  
        Dim currentHeaderitm As GridGroupHeaderItem = DirectCast(lnkReservations.NamingContainer, GridGroupHeaderItem)  
        For Each headerItem As GridGroupHeaderItem In RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)  
            If headerItem.GroupIndex <> currentHeaderitm.GroupIndex Then  
                Dim children As GridItem() = headerItem.GetChildItems()  
                For Each child As GridItem In children  
                    Dim dataItem As GridDataItem = TryCast(child, GridDataItem)  
                    dataItem.Visible = False  
                Next  
                headerItem.Visible = False  
            End If  
        Next  
        RadGrid1.ExportSettings.OpenInNewWindow = True  
        RadGrid1.ExportSettings.ExportOnlyData = True  
        RadGrid1.MasterTableView.ExportToPdf()  
    End Sub  
Private Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.PreRender
'Get items count in each group 
 Dim total2 As Integer = 0
  
For Each item As GridGroupHeaderItem In RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)
  
Dim dataItem As GridDataItem = Nothing 
For Each Gitem As GridItem In item.GetChildItems()
If TypeOf Gitem Is GridDataItem Then 
dataItem = TryCast (Gitem, GridDataItem)
If Not dataItem Is Nothing Then 
total2 += 1
End 
Next 
item.DataCell.Text = " Records " & total2 & " " & item.DataCell.Text
  
total2 = 0
Next 
  
  
End Sub 
 
  

 

If i take out Prerender and try eventhough lnkreservation finction is called it is not exported to pdf or excel

Please suggest what changes i need to do

 

Thanks
Kavya 

 

0
Iana Tsolova
Telerik team
answered on 21 Feb 2011, 08:51 AM
Hello Kavya,

I checked your code and I can see that in the PreRender event handler you are overriding the content of the GridGroupHeaderItem. Therefore it is expected the LinkButton event to fire only in case you comment the PreRender code. However, a possible reason for the grid not to be exported is if it is ajaxified and you are not disabling ajax upon export. Can you confirm this is not the case?

Additionally, find more information on how to export from ajaxified grid in the below article:
http://www.telerik.com/help/aspnet-ajax/grdexportwithajax.html

All the best,
Iana
the Telerik team
0
Kavya
Top achievements
Rank 2
answered on 21 Feb 2011, 04:41 PM
Hello Iana

I still need Prerender for the count .Please can you suggest me how not to override the GridGroupHeaderItem but still append the count.

This is the way i ma trying to print each group

Dim

 

lnkReservations As LinkButton = DirectCast(sender, LinkButton)

 

 

 

Dim currentHeaderitm As GridGroupHeaderItem = DirectCast(lnkReservations.NamingContainer, GridGroupHeaderItem)

 

 

 

For Each headerItem As GridGroupHeaderItem In grid.MasterTableView.GetItems(GridItemType.GroupHeader)

 

 

 

If headerItem.GroupIndex <> currentHeaderitm.GroupIndex Then

 

 

Dim children As GridItem() = headerItem.GetChildItems()

 

 

 

For Each child As GridItem In children

 

 

 

Dim dataItem As GridDataItem = TryCast(child, GridDataItem)

 

 

dataItem.Visible =

False

 

 

Next

 

headerItem.Visible =

False

 

 

End If

 

 

Next

 

grid.ExportSettings.OpenInNewWindow =

True

 

grid.ExportSettings.ExportOnlyData =

True

 

grid.ExportSettings.Pdf.AllowPrinting =

True

 

grid.MasterTableView.ExportToExcel()

How to disable Ajax here in this code

Please suggest and Thank you for the quick response and great support

Thanks
Kavya




0
Kavya
Top achievements
Rank 2
answered on 21 Feb 2011, 05:50 PM
Hi Iana

As per your information i disabled Ajax and also added the count at cell(1) as print is added on Cell(0) on GroupHeaderItem. Its working great

The only other problem i have is the print is not working when i load grid fro first time.
For Ex if i have 3 groups and i click on print for 1st group it show as if its loading and the hides all other groups. But export doesn't come up then again when i click on print it prints.This is happening only when the RadGrid is loaded for the first time.

Please any suggestions

Thanks
Kavya
0
Iana Tsolova
Telerik team
answered on 23 Feb 2011, 11:36 AM
Hi Kavya ,

Can you try debugging the page and see if on first click of the export button, the server code for exporting the grid is executed at all? A possible reason for it not to be hit is javascript error. Check that out as well.

Greetings,
Iana
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Kavya
Top achievements
Rank 2
answered on 23 Feb 2011, 05:32 PM
Hi Iana

I tried to debug each step when print is called for the frist time for my surprise it actually goes to

lnkReservations_Command

function tries to hide all other groups but the export is not working.

I don't get any javascript error also.Please can you suggest what else i need to change
Below is the Group
Private Sub lnkReservations_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
        Try
            Dim lnkReservations As LinkButton = DirectCast(sender, LinkButton)
            Dim currentHeaderitm As GridGroupHeaderItem = DirectCast(lnkReservations.NamingContainer, GridGroupHeaderItem)
            For Each headerItem As GridGroupHeaderItem In grid.MasterTableView.GetItems(GridItemType.GroupHeader)
                If headerItem.GroupIndex <> currentHeaderitm.GroupIndex Then
                    Dim children As GridItem() = headerItem.GetChildItems()
                    For Each child As GridItem In children
                        Dim dataItem As GridDataItem = TryCast(child, GridDataItem)
                        dataItem.Visible = False
                    Next
                    headerItem.Visible = False
                End If
            Next
           grid.ExportSettings.OpenInNewWindow = True
            grid.EnableAJAX = False
            grid.ExportSettings.ExportOnlyData = True
           grid.ExportSettings.Pdf.AllowPrinting = True
            grid.MasterTableView.ExportToExcel()
                      
        Catch ex As Exception
            Throw New Exception("Print_btn_Click" & ex.Message)
  
        End Try
      
    End Sub
Print function code
0
Iana Tsolova
Telerik team
answered on 28 Feb 2011, 01:37 PM
Hi Kavya,

Please confirm you have followed the steps for disabling ajax described here. If the issue persists, I would ask you to send me a runnable page so I can debug it locally and thus try finding what went wrong.

Kind regards,
Iana
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Kavya
Top achievements
Rank 2
answered on 01 Mar 2011, 12:18 AM
Iana

I tried the link you sent me but not luck
As my grid is not residing in Updatepanel i just tried using

<

 

script type="text/javascript">

 

 

function onRequestStart(sender, args)

 

 

{

 

 

if (args.get_eventTarget().indexOf("chk_print") >= 0)

 

 

args.set_enableAjax(

false);

 

 

}

</

 

script>

 



Please let me know what else ican change
Do you want me to send client & server side code if that will help


Thanks
Kavya
0
Iana Tsolova
Telerik team
answered on 01 Mar 2011, 01:05 PM
Hi Kavya,

Indeed, can you specify how you ajaxified the grid in your case? You can share the code for that if applicable.

All the best,
Iana
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Kavya
Top achievements
Rank 2
answered on 01 Mar 2011, 04:19 PM
Iana

Actually i removed

EnableAJAX

 

="True" EnableAJAXLoadingTemplate="True"

 


And it is working perfectly. I didn't need Ajax for anything so now its working .

Iana thank you for the great support

Kavya
Tags
Grid
Asked by
debbie f
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
debbie f
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Zyguy
Top achievements
Rank 1
Christopher Taylor
Top achievements
Rank 1
Iana Tsolova
Telerik team
Kavya
Top achievements
Rank 2
Share this question
or