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:
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.
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.