Home Forums WinForms controls Xceed Grid for WinForms Exporting data from Xceed Grid Reply To: Exporting data from Xceed Grid

User (Old forums)
Member
Post count: 23064

Martha: The following code will export to a text file formatted for easy import to Excel. grdDisplay is the Xceed.Grid.GridControl object. It may not be _exactly_ what you wanted, but there’s some pretty cool stuff that surely will get to started. It serves our purpose just fine.

Mark Bonafe

public void Export2TabbedFile()
{
// Create the FileStream and StreamWriter object to write
// the recordset contents to file.
System.Windows.Forms.SaveFileDialog saveDialog = new System.Windows.Forms.SaveFileDialog();
saveDialog.Title = “Export Grid Contents to File:”;
saveDialog.DefaultExt = “txt”;
System.Windows.Forms.DialogResult result = saveDialog.ShowDialog(this);
string fileName;
if (result == System.Windows.Forms.DialogResult.OK)
{
fileName = saveDialog.FileName;
}
else
{
return;
}

System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Create);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Unicode);

// Write the field names (headers) as the first line in the text file.
for (int colIndex = 0; colIndex < grdDisplay.Columns.Count; colIndex++)
{
for (int col = 0; col < grdDisplay.Columns.Count; col++)
{
if (grdDisplay.Columns[col].Visible)
{
if (grdDisplay.Columns[col].VisibleIndex == colIndex)
{
WriteText(sw,grdDisplay.Columns[col].Title + “\t”);
break;
}
}
}
}
WriteTextLine(sw,””);

// Get the rows as sorted in the grid
if (grdDisplay.Groups.Count > 0)
{
for (int grp = 0; grp < grdDisplay.Groups.Count; grp++)
{
WriteTextLine(sw,grdDisplay.Groups[grp].GroupByRowTitle + “: ” + grdDisplay.Groups[grp].Title);
ExportGroup(grdDisplay, grdDisplay.Groups[grp], sw);
}
}
else
{
Xceed.Grid.Collections.ReadOnlyDataRowList gridRows = grdDisplay.GetSortedDataRows(true);
ExportRows(grdDisplay, gridRows, sw);
}

// Close the FileStream.
fs.Close();
MessageBox.Show(“File exported!”,”Success”,System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Information);
}

private void ExportGroup(Xceed.Grid.GridControl exportGrid, Xceed.Grid.Group exportGroup, System.IO.StreamWriter sw)
{
for (int grp = 0; grp < exportGroup.Groups.Count; grp++)
{
sw.WriteLine(exportGroup.Groups[grp].GroupByRowTitle + “: ” + exportGroup.Groups[grp].Title);
if (exportGroup.Groups[grp].Groups.Count > 0)
ExportGroup(exportGrid, exportGroup.Groups[grp], sw);
else
{
Xceed.Grid.Collections.ReadOnlyDataRowList gridRows = exportGroup.Groups[grp].GetSortedDataRows(false);
ExportRows(exportGrid, gridRows, sw);
}
}
}

private void ExportRows(Xceed.Grid.GridControl exportGrid, Xceed.Grid.Collections.ReadOnlyDataRowList gridRows, System.IO.StreamWriter sw)
{
int col;
int row;
int colIndex = 0;
for (row = 0; row < gridRows.Count; row++)
{
if (gridRows[row].Visible)
{
for (colIndex = 0; colIndex < exportGrid.Columns.Count; colIndex++)
{
for (col = 0; col < exportGrid.Columns.Count; col++)
{
if (exportGrid.Columns[col].Visible)
{
if (exportGrid.Columns[col].VisibleIndex == colIndex)
{
if (gridRows[row].Cells[col].CellViewer == null)
sw.Write(gridRows[row].Cells[col].Value.ToString() + “\t”);
else
{
// First, figure out which type of CellViewer is being used
// Second, perform a lookup on it
// Third, write the text
string text2Write = gridRows[row].Cells[col].Value.ToString();
if (gridRows[row].Cells[col].CellViewer is Xceed.Grid.Editors.GridComboBox)
{
Xceed.Grid.Editors.GridComboBox cbxCombo;
cbxCombo = (Xceed.Grid.Editors.GridComboBox)gridRows[row].Cells[col].CellViewer;
System.Data.DataView dv;
try
{
dv = (System.Data.DataView)cbxCombo.DataSource;
text2Write = FindComboEntry(dv, gridRows[row].Cells[col].Value,cbxCombo.ValueMember, cbxCombo.DisplayMember);
}
catch (InvalidCastException)
{
try
{
dv = ((System.Data.DataTable)cbxCombo.DataSource).DefaultView;
text2Write = FindComboEntry(dv, gridRows[row].Cells[col].Value, cbxCombo.ValueMember, cbxCombo.DisplayMember);
}

Imported from legacy forums. Posted by MBonafe (had 710 views)