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

Problem Exporting to Excel after i filter the collection

1 Answer 48 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Miguel Rios
Top achievements
Rank 1
Miguel Rios asked on 28 Apr 2010, 11:26 PM
Hi Telerik team,

I am having a problem once i try to export the grid information to Excel. I works fine when i export the grid directly, but i have some empty elements in the grid that i'd like to erase before i export it. To filter the grid, i create a new grid in code behind, filter the one that i have with the data with a foreach statement with the condition that i want, the elements that pass the condition are added to the new grid and i export this newly created grid.

I send a code snippet with the operation that i'm doing:

        private void txtExportExcel_1_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)  
        {  
              
            RadMenuItem item = (RadMenuItem)sender;       
 
              
            SaveFileDialog dialog = new SaveFileDialog()  { DefaultExt = "xls"Filter = String.Format("{1} files (*.{0})|*.{0}", "xls", "Excel"), FilterIndex = 1  };  
              
 
              
            if (dialog.ShowDialog() == true)  
            {  
                  
                using (Stream stream = dialog.OpenFile())  
                {  
                      
                    using (StreamWriter sw = new StreamWriter(stream, Encoding.Unicode))  
                    {  
                          
                        switch(Convert.ToInt32(item.Tag))  
                        {  
                              
                            case 0:  
                                {  
                                    RadGridView rgv = new RadGridView();  
                                    rgv.AutoGenerateColumns = false;  
                                    ObservableItemCollection<myObject> observable = new ObservableItemCollection<myObject>();  
                                    foreach (var itm in dataGrid.Items)  
                                    {  
                                        var type = item.GetType();  
                                        var XpropertyProp = type.GetProperty("Xproperty");  
                                        var XValue = XpropertyProp .GetValue(item, null);  
                                        if ((int)XValue > 0)  
                                        {  
                                            observable.Add((myObject)itm);  
                                        }  
                                    }  
 
                                    GridViewDataColumn clm = new GridViewDataColumn();  
                                    clm.Header = "YProperty";  
                                    clm.IsResizable = true;  
                                    clm.DataMemberBinding = new Binding("YProperty");  
                                    rgv.Columns.Add(clm);  
 
                                    GridViewDataColumn clm1 = new GridViewDataColumn();  
                                    clm1.Header = "ZProperty";  
                                    clm1.IsResizable = true;  
                                    clm1.DataMemberBinding = new Binding("ZProperty");  
                                    rgv.Columns.Add(clm1);  
 
                                    rgv.ItemsSource = observable;  
                                    sw.Write(rgv.ToHtml());  
                                    break;  
                                }  
                            case 1:  
                                {  
                                    sw.Write(otherGrid.ToHtml());   
                                    break;  
                                }  
                            case 2:  
                                {  
                                    sw.Write(thirdGrid.ToHtml());   
                                    break;  
                                }  
                        }   
                    }  
 
                    // Closes the stream   
                    stream.Close();  
                }  
            }  
        } 
 

Tha tag is used to distinguish between 3 grids that i have in my project. The result of the exported grid is a cell in the .xml file that have the next characters : ÿþ

I dont know what is happening... any ideas here?

Thank you.

1 Answer, 1 is accepted

Sort by
0
Miguel Rios
Top achievements
Rank 1
answered on 29 Apr 2010, 04:12 AM
Hello again,

I already fix the problem, it was a mistake of my own, thank you for your attention.
Tags
GridView
Asked by
Miguel Rios
Top achievements
Rank 1
Answers by
Miguel Rios
Top achievements
Rank 1
Share this question
or