RadControls for ASP.NET AJAX
General information
CSV format is based on a plain text - it is widely used to display simple tabular data. Due to its simplicity and portability, it can be seen in various platforms - web, desktop, mobile, embedded, etc.
By default, RadGrid's CSV engine encloses all the data within double quotes to allow the usage of commas, new lines and so on.
Note |
|---|
GridExporting event allows the developer to change the generated file directly. This approach could be used to achieve some custom functionality that is not supported out of the box. For example you can use it to add header text to the output and then save it to TXT format (since it won't be valid CSV anymore).
|
Note |
|---|
Depending on your local settings, the default column separator might be a comma or a semicolon. Excel won't be able to properly separate the columns when the column separator is not valid for the current culture.
|
Note |
|---|
Note, that if you set IgnorePaging="true" and ExportOnlyData="true", the contents (text) of all template columns will be removed but the columns will be still visible in the exported file.
|
Caution |
|---|
The header text in the CSV file is generated from the column's HeaderText and not from the header cell's content!
|
CSV specific settings
RadGrid exposes the following properties for customizing the output:
Name | Options | Description |
|---|
ColumnDelimiter | Colon, Comma, NewLine, Semicolon, Tab, VerticalBar | Used to specify the column delimiter |
RowDelimiter | Colon, Comma, NewLine, Semicolon, Tab, VerticalBar | Used to specify the row delimiter |
FileExtension | File extension string |
Useful when the developer wishes to change the file extension. Since the CSV format is text-based, it is often saved as TXT instead of CSV |
EncloseDataWithQuotes | True/False | Determines whether the exported values are enclosed with quotes. |
CopyASPX
<ExportSettings>
<Csv ColumnDelimiter="Tab" RowDelimiter="NewLine" FileExtension="TXT" EncloseDataWithQuotes="true" />
</ExportSettings> Note |
|---|
Microsoft Excel parses the cell values automatically depending on the local settings. For example the string 19/05 might change to 19.May automatically. The only workaround would be to insert a sign of equality (=) before the relevant string. For example: "012" should be modified as ="012".
|
Unicode support
RadControls for ASP.NET AJAX Q2 2009 SP1 and later fully supports Unicode symbols in both left-to-right and right-to-left modes. RadGrid automatically saves the file in UTF-8 encoding.
Note |
|---|
If you open the CSV file in some editors, you will notice some strange characters at the beginning. This is BOM (Byte Order Mark) header - it marks the file content as Unicode. These characters wouldn't be visible under normal circumstances.
|
Exporting unsupported controls
Since CSV is text-based format it doesn't support images or complex structures like tables. It is possible to replace such content manually by removing the unwanted objects before exporting or on ItemDataBound/ItemCreated events:
CopyASPX
<telerik:GridTemplateColumn UniqueName="MyColumn" HeaderText="MyColumn">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl="~/img.png" AlternateText="AltText" />
</ItemTemplate>
</telerik:GridTemplateColumn>
CopyC#
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
Image img = item["MyColumn"].FindControl("Image1") as Image;
item["MyColumn"].Text = img.AlternateText;
}
RadGrid1.MasterTableView.ExportToCSV();
}
CopyVB.NET
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
For Each item As GridDataItem In RadGrid1.MasterTableView.Items
Dim img As Image = TryCast(item("MyColumn").FindControl("Image1"), Image)
item("MyColumn").Text = img.AlternateText
Next
RadGrid1.MasterTableView.ExportToCSV()
End Sub
This approach won't work if your RadGrid rebinds before export. In such cases (for instance, when using IgnorePaging="true") the developer should put the code on ItemCreated/ItemDataBound:
CopyC#
bool isExport = false;
protected void Button1_Click(object sender, EventArgs e)
{
isExport = true;
RadGrid1.MasterTableView.ExportToCSV();
}
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem && isExport)
{
TableCell cell = (e.Item as GridDataItem)["myColumn"];
Image img = cell.FindControl("Image1") as Image;
cell.Text = img.AlternateText;
}
}
CopyVB.NET
Private isExport As Boolean = FalseProtected
Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
isExport = True
RadGrid1.MasterTableView.ExportToCSV()
End Sub
Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid1.ItemCreated
If TypeOf e.Item Is GridDataItem AndAlso isExport Then
Dim cell As TableCell = (TryCast(e.Item, GridDataItem))("myColumn")
Dim img As Image = TryCast(cell.FindControl("Image1"), Image)
cell.Text = img.AlternateText
End If
End Sub Note |
|---|
Note that you don't need to set isExport="false" explicitly because the changes we perform will affect only the response streamed to the client. In other words, the user won't see any change to the actual page.
|
Exporting the selected rows only
You can hide the unselected rows and export the rest in a manner similar to the example above. This approach is applicable also for HTML Excel/Word and PDF.
CopyC#
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
item.Visible = item.Selected;
RadGrid1.MasterTableView.ExportToCSV();
}
CopyVB.NET
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
For Each item As GridDataItem In RadGrid1.MasterTableView.Items
item.Visible = item.Selected
Next RadGrid1.MasterTableView.ExportToCSV()
End Sub
Hiding columns
You can use the HideStructureColumns property to hide GridRowIndicatorColumn, GridExpandColumn and GridGroupSplitterColumn. For the other columns types, you can use the following approach:
CopyC#
protected void Button1_Click(object sender, EventArgs e)
{
RadGrid1.MasterTableView.GetColumn("C2").Visible = false;
RadGrid1.MasterTableView.ExportToCSV();
}
CopyVB.NET
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
RadGrid1.MasterTableView.GetColumn("C2").Visible = False
RadGrid1.MasterTableView.ExportToCSV()
End Sub
Hiding items
There are two common ways to hide an item.
CopyC#
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.ExportToCsvCommandName)
RadGrid1.MasterTableView.Items[2].Visible = false;
}
CopyVB.NET
Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) Handles RadGrid1.ItemCommand
If e.CommandName = RadGrid.ExportToCsvCommandName Then
RadGrid1.MasterTableView.Items(2).Visible = False
End If
End Sub
CopyC#
protected void Button1_Click(object sender, EventArgs e)
{
RadGrid1.MasterTableView.Items[2].Visible = false;
RadGrid1.MasterTableView.ExportToCSV();
}
CopyVB.NET
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
RadGrid1.MasterTableView.Items(2).Visible = False
RadGrid1.MasterTableView.ExportToCSV()
End Sub
CopyC#
bool isCsvExport = false;
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.ExportToCsvCommandName)
isCsvExport = true;
}
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
if (isCsvExport && e.Item.ItemIndex == 2)
e.Item.Display = false;
}
CopyVB.NET
Private isCsvExport As Boolean = FalseProtected
Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
If e.CommandName = RadGrid.ExportToCsvCommandName Then
isCsvExport = True
End If
End Sub
Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs)
If isCsvExport AndAlso e.Item.ItemIndex = 2 Then
e.Item.Display = False
End If
End Sub
CSV Export Q & A
Q: Can I export content from GridTemplateColumn?
A: Yes, as long as it contain a binding expression or regular Label control and ExportOnlyData is set to "false".
Q: How to export GridCheckBoxColumn?
A: You have to set ExportOnlyData to "false"
Unsupported scenarios