Checkbox as header in GridItemTemplate

3 posts, 1 answers
  1. kmccusker
    kmccusker avatar
    11 posts
    Member since:
    Sep 2006

    Posted 28 Mar 2012 Link to this post

    Hi!

    I could use some assistance if anyone is able to help. In my application I have a grid displaying a number of columns. There is a requirement for the users to be able to select any number of columns and then export the selected columns to PDF or Excel. To enable selection of columns, I added a checkbox into the HeaderTemplate of a GridItemTemplateColumn for each column.

    By the way, the code displayed in this is a simplified version I created in an attempt to identify the issue. :)

    Here is the markup for my RadGrid
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false">           
          <MasterTableView>
              <Columns>
                  <telerik:GridTemplateColumn UniqueName="ID"
                      AllowFiltering="true"
                      HeaderStyle-Width="100"
                      SortExpression="ID"
                      FilterControlWidth="50"
                      HeaderText="ID">
                      <HeaderTemplate>
                          <asp:CheckBox ID="IDCheck" runat="server" CssClass="HeaderCheckBox" Text="ID" />
                      </HeaderTemplate>
                      <ItemTemplate>
                          <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                      </ItemTemplate>
                  </telerik:GridTemplateColumn>
                  <telerik:GridTemplateColumn UniqueName="Name"
                      AllowFiltering="true"
                      HeaderStyle-Width="100"
                      SortExpression="ID"
                      FilterControlWidth="50"
                      HeaderText="Name">
                      <HeaderTemplate>
                          <asp:CheckBox ID="NameCheck" runat="server" CssClass="HeaderCheckBox" Text="Name" />
                      </HeaderTemplate>
                      <ItemTemplate>
                          <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                      </ItemTemplate>
                  </telerik:GridTemplateColumn>
                  <telerik:GridTemplateColumn UniqueName="RandomText"
                      AllowFiltering="true"
                      HeaderStyle-Width="100"
                      SortExpression="RandomText"
                      FilterControlWidth="50"
                      HeaderText="RandomText">
                      <HeaderTemplate>
                          <asp:CheckBox ID="RandomTextCheck" runat="server" CssClass="HeaderCheckBox" Text="Random" />
                      </HeaderTemplate>
                      <ItemTemplate>
                          <asp:Label ID="RandomTextLabel" runat="server" Text='<%# Eval("RandomText") %>'></asp:Label>
                      </ItemTemplate>
                  </telerik:GridTemplateColumn>
              </Columns>
          </MasterTableView>
      </telerik:RadGrid>
      <asp:Button ID="PDFExportButton" runat="server" OnClick="ExportToPDF" Text="Export to PDF" />
      <asp:Button ID="ExcelExportButton" runat="server" OnClick="ExportToExcel" Text="Export to Excel" />

    And here is the code behind:

    public partial class Default : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            RadGrid1.DataSource = GetDataSource();
            RadGrid1.DataBind();
     
            base.OnInit(e);
        }
         
        protected void Page_Load(object sender, EventArgs e)
        {
             
        }
     
        protected List<Person> GetDataSource()
        {
            List<Person> peeps = new List<Person>();
     
            for (int i = 0; i < 10; i++)
            {
                Person p = new Person
                {
                    Name = "Test" + i.ToString(),
                    ID = i,
                    RandomText = DateTime.Now.Millisecond.ToString()
                };
     
                peeps.Add(p);
            }
     
            return peeps;
        }
     
        protected void ExportToPDF(object sender, EventArgs e)
        {
            RadGrid1.ExportSettings.ExportOnlyData = true;
            RadGrid1.ExportSettings.HideStructureColumns = true;
            RadGrid1.ExportSettings.IgnorePaging = true;
            //RadGrid1.ExportSettings.OpenInNewWindow = true;
            RadGrid1.MasterTableView.ExportToPdf();
        }
     
        protected void ExportToExcel(object sender, EventArgs e)
        {
            RadGrid1.ExportSettings.ExportOnlyData = true;
            RadGrid1.ExportSettings.HideStructureColumns = true;
            RadGrid1.ExportSettings.IgnorePaging = true;
            //RadGrid1.ExportSettings.OpenInNewWindow = true;
            RadGrid1.MasterTableView.ExportToExcel();
        }
    }

    The issue I am having is that when the columns are exported instead of the HeaderText being displayed, "False" is displayed. I've attached a screenshot of the PDF output.

    To sum it up: I want the column's header text to display in the exported files instead of the text "False", but I can't figure out what I am doing wrong or if there is a better way to achieve this.

    Thanks.
    Kevin


  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 29 Mar 2012 Link to this post

    Hello,

    I think the best way for this scenario would be to create a hidden GridTemplateColumn that you make visible when exporting. Please try this approach.
    aspx:
    <telerik:GridTemplateColumn UniqueName="ID" AllowFiltering="true" HeaderStyle-Width="100" SortExpression="FirstName" FilterControlWidth="50" HeaderText="ID">
      <HeaderTemplate>
         <asp:CheckBox ID="IDCheck" runat="server" CssClass="HeaderCheckBox" />
      </HeaderTemplate>
      <ItemTemplate>
          <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
      </ItemTemplate>
    </telerik:GridTemplateColumn>
    <telerik:GridTemplateColumn UniqueName="IDForExport" AllowFiltering="true" HeaderStyle-Width="100" SortExpression="FirstName" FilterControlWidth="50" HeaderText="ID" Visible="false">
       <ItemTemplate>
          <asp:Label ID="IDLabelForExport" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
       </ItemTemplate>
    </telerik:GridTemplateColumn>
    C#:
    protected void ExportToExcel(object sender, EventArgs e)
    {
      RadGrid1.ExportSettings.ExportOnlyData = true;
      RadGrid1.ExportSettings.IgnorePaging = true;
      (RadGrid1.MasterTableView.GetColumn("IDForExport") as GridTemplateColumn).Visible = true;
      (RadGrid1.MasterTableView.GetColumn("ID") as GridTemplateColumn).Visible = false;
       RadGrid1.MasterTableView.ExportToExcel();
    }
    I hope that helps.

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. kmccusker
    kmccusker avatar
    11 posts
    Member since:
    Sep 2006

    Posted 29 Mar 2012 Link to this post

    Thank you for the response. I'll test it out and let you know how it goes!

    EDIT: Thanks again Princy! That worked out just fine. If you ever find yourself in Phoenix, AZ send me a message and I'll buy you a beverage of your choice. :)
Back to Top