6 Answers, 1 is accepted
Do not know if it is available out of the box, the following is from my codebase
public
static
System.IO.TextWriter ExportCSV(System.IO.TextWriter sw, Telerik.WinControls.UI.RadGridView rgv,
bool
withHeaders)
{
string
rowSep =
"\r\n"
;
string
listSep = CultureInfo.CurrentCulture.TextInfo.ListSeparator;
string
sep =
""
;
string
quoteSeq =
"\""
;
string
quoteSeqEscape =
"\"\""
;
Action newLine = () =>
{
if
(sep !=
""
)
sw.Write(rowSep);
sep =
""
;
};
Action<
string
> newCell = (x) =>
{
sw.Write(sep);
//field separator
if
(x.Contains(listSep) || x.Any(a => a < 32))
{
sw.Write(quoteSeq);
sw.Write(x.Replace(quoteSeq, quoteSeqEscape));
sw.Write(quoteSeq);
}
else
sw.Write(x);
sep = listSep;
};
if
(withHeaders)
{
foreach
(var c
in
rgv.Columns)
{
if
(c.IsVisible)
{
newCell(c.HeaderText);
}
}
}
foreach
(var row
in
rgv.SelectedRows)
{
newLine();
foreach
(var c
in
rgv.Columns)
{
if
(c.IsVisible)
{
newCell(row.Cells[c.Index].Value?.ToString() ??
""
);
}
}
}
return
sw;
}
Hi,
what I mean is to use the native exportation of the grid described here to perform the action:
http://docs.telerik.com/devtools/winforms/gridview/exporting-data/export-to-csv
maybe this methods has a filter or a way to export only selected rows instead all table.
Thanks
Francisco
Thank you for writing.
We do not have any build in functionality for exporting the selected rows only. In this case, you can continue to use your solution. Another approach would be to use the SpreadProecessing library. We have an example which shows how you can create similar custom exporter in our demo application (see attached image).
I hope this helps. Should you have any other questions do not hesitate to ask.
Regards,
Dimitar
Telerik
Hi,
thank you very much for your help, i will take a look to the "SpreadProecessing library", anyway ... I am thinking in to create a new one hidden grid and after invoke the exportation, add the selected rows to this new grid and make the export from the hidden grid. I think that this could be easy and enought for me.
Francisco
Thank you for writing back.
It is pretty simple to manually create the file as well. For example:
private
void
radButton1_Click(
object
sender, EventArgs e)
{
Workbook workbook =
new
Workbook();
Worksheet worksheet = workbook.Worksheets.Add();
int
rowIndex = 0;
foreach
(var row
in
radGridView1.SelectedRows)
{
for
(
int
i = 0; i < row.Cells.Count; i++)
{
object
value = row.Cells[i].Value;
worksheet.Cells[rowIndex, i].SetValue(value.ToString());
}
rowIndex++;
}
string
fileName = @
"D:\SampleFile.csv"
;
IWorkbookFormatProvider formatProvider =
new
CsvFormatProvider();
using
(FileStream output =
new
FileStream(fileName, FileMode.Create))
{
formatProvider.Export(workbook, output);
}
}
Please let me know if there is something else I can help you with.
Dimitar
Telerik
Old topic but to whom it may apply:
Just before export I hide all the non selected rows, export with
exporter.HiddenRowOption = Export.HiddenOption.DoNotExport
after that I make them visible again.works like a charm.