export boolean columns from RadGridView

11 posts, 1 answers
  1. Gani
    Gani avatar
    8 posts
    Member since:
    Mar 2013

    Posted 11 Apr 2014 Link to this post

    My item source to grid is a class has about 20 columns. I have two bool columns.

    public bool IsPassed {get;set;}
    public bool HasPassport {get;set;}

    when the grid is loaded or searhced these two cols display an image icon when the value is true.
    while exporting to excel if IsPassed = true..then I have to show "Yes" in the excel else empty
    if HasPassport = true, then I have to display some string value like "hdgfjhgh"

    can anyone give me an example how to do it ?


  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 14 Apr 2014 Link to this post

    Hi,

    To override the default export, you can subscribe for the ElementExporting event of RadGridView and set the e.Value to be exported as you would like to.

    Regards,
    Didie
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Gani
    Gani avatar
    8 posts
    Member since:
    Mar 2013

    Posted 14 Apr 2014 in reply to Dimitrina Link to this post

    we are following the MVVM Prism, here I have a button bind to viewmodel, in the viewmodel we have the access to the grid. 
    Am using CSExcelExportUtilities.GetExcelMLFromGridView()
                     CSExcelExportUtilities.ExportToExcel()
    this method to export to excel. 

    Can I override the value here as i have access to the grid ? How to do that
  5. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 15 Apr 2014 Link to this post

    Hi,

    If you have access to RadGridView, then you can subscribe for its ElementExporting event. The only way to override the value that is exported is through this event (setting a proper e.Value instead the one that is coming by default). You can check our Exporting WPF Demos for some examples on how to customize the export.

    Regards,
    Didie
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  6. Gani
    Gani avatar
    8 posts
    Member since:
    Mar 2013

    Posted 15 Apr 2014 in reply to Dimitrina Link to this post

    I want to update the content in that column, with the e.Value i can update the column header or style etc. In the grid am showing a image icon now want to convert that some string "*****" while exporting. 

    Thanks
  7. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 18 Apr 2014 Link to this post

    Hello,

    I am not sure I understand your question. You say with the e.Value you can update the column header or style etc. Actually you can change any value that is going to be exported. Do you have a problem changing the value when you have subscribed for the ElementExporting event?

    Regards,
    Didie
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  8. Gani
    Gani avatar
    8 posts
    Member since:
    Mar 2013

    Posted 24 Apr 2014 in reply to Dimitrina Link to this post

    Yes I have the problem , when I update the e.value it is reflected in the header. Lets say I have a column called "File"  we show a file icon in the radgrid results when it is true, while exporting to excel i want to update that to "Yes" to a string.

    File || Name ||
    ----------------------------
    Yes || xyx ||
  9. Answer
    Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 28 Apr 2014 Link to this post

    Hello,

    In order to not override the headers you need to change the e.Value only when the e.Element is ExportElement.Cell.

    For example:
    private void clubsGrid_ElementExporting(object sender, GridViewElementExportingEventArgs e)
    {
        if (e.Element == ExportElement.Cell)
        {
            var column = e.Context as GridViewDataColumn;
            if (column != null && column.Header.ToString()=="File")
            {
                  if (e.Value == true)
                  {
                e.Value = "Yes";
                  }
            }
        }
     
    }

     How does this work for you?

    Regards,
    Didie
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  10. Gani
    Gani avatar
    8 posts
    Member since:
    Mar 2013

    Posted 28 Apr 2014 in reply to Dimitrina Link to this post

    Thanks, it worked.
  11. jlj30
    jlj30 avatar
    101 posts
    Member since:
    Jan 2011

    Posted 16 May in reply to Dimitrina Link to this post

    Hi,

    I have the same requirement, but am using Format="Xlsx" and trying to accomplish this same task using the OnInfrastructureExporting event handler as follows:

    protected void grdUserAdmin_InfrastructureExporting(object sender, GridInfrastructureExportingEventArgs e)
    {
        var colCount = e.ExportStructure.Tables[0].Columns.Count;
        var rowCount = e.ExportStructure.Tables[0].Rows.Count;
        ExportStyle headerStyle = new ExportStyle();
        headerStyle.ForeColor = Color.White;
        headerStyle.BackColor = Color.Blue;
        headerStyle.Font.Bold = true;
     
        // Set the header style for all columns
        for (var i = 1; i <= colCount; i++)
        {
            e.ExportStructure.Tables[0].Cells[i, 1].Style = headerStyle;
            e.ExportStructure.Tables[0].Columns[i].Width = 200; // Would like auto-width !!
        }
     
        // Place an "X" in cells assigned to a boolean data field if value is true
        var headerText = "";
        for (var c = 1; c <= colCount; c++)
        {
            for (var r = 1; r <= rowCount; r++)
            {
                if (r == 1)
                {
                    headerText = e.ExportStructure.Tables[0].Cells[c, r].Value.ToString();
                    continue;
                }
                if (headerText == "Active")
                {
                    var x = e.ExportStructure.Tables[0].Cells[c, r].Value.ToString();
                    System.Diagnostics.Debug.WriteLine("Cell Value: " + x);
                    if (Convert.ToBoolean(e.ExportStructure.Tables[0].Cells[c, r].Value.ToString()))
                        e.ExportStructure.Tables[0].Cells[c, r].Value = "X";
                }
            }
        }
     
    }

    The value for the variable x above is always "" or null (hard to tell in the Output section of VS).

    I know that my data for column Active should have all true values.

    The conversion to boolean fails!

    I think I'm close.

    What am I doing wrong?

    Thanks in advance for any advice.

    Jim

  12. Stefan X1
    Admin
    Stefan X1 avatar
    523 posts

    Posted 18 May Link to this post

    Hi Jim,

    As this forum thread is for the Telerik UI for WPF suite, can you please repost your question in the relevant forum section which I believe is the UI for ASP.NET AJAX?

    Thank you in advance for your cooperation.

    All the best,
    Stefan X1
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for WPF is Visual Studio 2017 Ready