Export to csv "String" Key Word Apperaing In Output

2 posts, 1 answers
  1. Shivam
    Shivam avatar
    5 posts
    Member since:
    Oct 2011

    Posted 13 Dec 2011 Link to this post

    Hi,

    I have The Below Code for Exporting My Various RadGridView inside RadTabcontol to a .csv file on local:

    IList<RadGridView> AllRadGridViews { get; set; }
            public void ExportDataLog(object pResource)
            {
                try
                {
                    AllRadGridViews = new ObservableCollection<RadGridView>();
     
                    GridViewCsvExportOptions GridViewCsvExportOptions = new GridViewCsvExportOptions();
                    GridViewCsvExportOptions.Format = ExportFormat.Csv;
                    GridViewCsvExportOptions.ShowColumnHeaders = true;
                    GridViewCsvExportOptions.ShowColumnFooters = true;
                    GridViewCsvExportOptions.ShowGroupFooters = false;
     
     
     
                    //the empty rows in grid doesnot have to open the save dialog              
                    if (pResource.GetType() == typeof(Grid))
                    {
     
                        SaveFileDialog saveLogFileDialog = new SaveFileDialog()
                        {
                            DefaultExt = "csv",
                            Filter = "CSV Files (*.csv)|*.csv|All files (*.*)|*.*",
                            FilterIndex = 1
                        };
                        if (saveLogFileDialog.ShowDialog() == true)
                        {
                            using (System.IO.Stream stream = saveLogFileDialog.OpenFile())
                            {
                                if (((Grid)(pResource)).Children.ElementAt(0).GetType() == typeof(RadTabControl))
                                {
     
                                    RadTabControl RadTabControl = new RadTabControl();
                                    RadTabControl = (RadTabControl)((Grid)(pResource)).Children.ElementAt(0);
     
     
                                    RadTabItem SummaryRadTabItem = new RadTabItem();
                                    RadTabItem DataRadTabItem = new RadTabItem();
                                    RadTabItem BitmapRadTabItem = new RadTabItem();
                                     
                                    RadGridView radTextBoxData = new RadGridView();
     
                                    if (radTextBoxData.Items.Count > 0)
                                        radTextBoxData.Items.Clear();
     
                                    radTextBoxData.ShowColumnHeaders = false;
     
                                    radTextBoxData.Items.Add("Name: " + DeviceName);
                                    radTextBoxData.Items.Add("Model: " + DeviceModel);
                                     
                                    if (radTextBoxData.Items.Count > 0)
                                        AllRadGridViews.Add(radTextBoxData);
     
                                    //SummaryRadTabItem
                                    SummaryRadTabItem = (RadTabItem)((System.Windows.UIElement)(((System.Windows.Controls.ContentControl)(RadTabControl.ChildrenOfType<RadTabItem>().ElementAt(0)))));
     
                                    Grid grdSummaryGrid = new Grid();
                                    grdSummaryGrid = (Grid)((System.Windows.FrameworkElement)(((System.Windows.Controls.ContentControl)(SummaryRadTabItem)).Content));
     
                                    RadGridView radgrdSummaryGrid = new RadGridView();
                                    RadGridView radgrdNFPA110Summary = new RadGridView();
                                    RadGridView radgrdNFPAExtendedSummary = new RadGridView();
                                    radgrdSummaryGrid = (RadGridView)grdSummaryGrid.FindName("summaryGrid");
                                    radgrdNFPA110Summary = (RadGridView)grdSummaryGrid.FindName("grdNFPA110Summary");
                                    radgrdNFPAExtendedSummary = (RadGridView)grdSummaryGrid.FindName("grdNFPAExtendedSummary");
     
                                    if (radgrdSummaryGrid.Items.Count > 0)
                                        AllRadGridViews.Add(radgrdSummaryGrid);
                                    if (radgrdNFPA110Summary.Items.Count > 0)
                                        AllRadGridViews.Add(radgrdNFPA110Summary);
                                    if (radgrdNFPAExtendedSummary.Items.Count > 0)
                                        AllRadGridViews.Add(radgrdNFPAExtendedSummary);
     
                                    RadGridView radGenset = new RadGridView();
                                    RadGridView radATS = new RadGridView();
                                    radGenset = (RadGridView)grdSummaryGrid.FindName("radSummaryGensetData");
                                    radATS = (RadGridView)grdSummaryGrid.FindName("radSummaryATSData");
     
                                    if (radGenset.Items.Count > 0)
                                        AllRadGridViews.Add(radGenset);
                                    else if (radATS.Items.Count > 0)
                                        AllRadGridViews.Add(radATS);
     
                                    RadGridView radgrdGensetNFPA = new RadGridView();
                                    RadGridView radgrdATSNFPA = new RadGridView();
                                    RadGridView radgrdGensetNFPAExtended = new RadGridView();
                                    RadGridView radgrdATSNFPAExtended = new RadGridView();
                                    radgrdGensetNFPA = (RadGridView)grdSummaryGrid.FindName("radgrdSummaryGensetNFPA");
                                    radgrdATSNFPA = (RadGridView)grdSummaryGrid.FindName("radgrdSummaryATSNFPA");
                                    radgrdGensetNFPAExtended = (RadGridView)grdSummaryGrid.FindName("radgrdSummaryGensetNFPAExtended");
                                    radgrdATSNFPAExtended = (RadGridView)grdSummaryGrid.FindName("radgrdSummaryATSNFPAExtended");
     
                                    if (radgrdGensetNFPA.Items.Count > 0)
                                        AllRadGridViews.Add(radgrdGensetNFPA);
                                    else if (radgrdATSNFPA.Items.Count > 0)
                                        AllRadGridViews.Add(radgrdATSNFPA);
                                    if (radgrdGensetNFPAExtended.Items.Count > 0)
                                        AllRadGridViews.Add(radgrdGensetNFPAExtended);
                                    else if (radgrdATSNFPAExtended.Items.Count > 0)
                                        AllRadGridViews.Add(radgrdATSNFPAExtended);
     
                                    RadGridView radCommentsData = new RadGridView();
                                    if (radCommentsData.Items.Count > 0)
                                        radCommentsData.Items.Clear();
     
                                     
                                    radCommentsData.Items.Add("Comments: " + ReportComments);
                                  
                                    
     
                                    if (radCommentsData.Items.Count > 0)
                                        AllRadGridViews.Add(radCommentsData);
     
                                   
                                    foreach (var exportgrid in AllRadGridViews)
                                    {
                                        stream.Flush();
                                        exportgrid.Export(stream, GridViewCsvExportOptions);
                                        stream.Flush();
                                    }
                                }
     
                            }
     
                        }
                    }
                }
                catch
                {
     
                }
            }
     I am using pure MVVM approach for my functionality,
    But in the output file I am getting the "String" over the above BOLD lines:

    Please Provide your Input on how to get rid of this "String" keyword
    PFA ScreenShot of my .csv,

    Regards,
    SHIVAM
  2. Answer
    Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 14 Dec 2011 Link to this post

    Hello Shivam,

     It seems that an empty header row is exported. You need to subscribe for the ElementExporting event of the RadGridView and cancel the export of ExportElement.HeaderRow. For example:

    void radTextBoxData_ElementExporting(object sender, GridViewElementExportingEventArgs e)
           {
               if (e.Element == ExportElement.HeaderRow)
               {
                   e.Cancel = true;
               }
           }

    Please try how this works.

    Kind regards,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top