I'm using Infragistics's UltraGrid widget to create Excel-like grid. I am trying to copy value from cell and paste it to Excel. That works fine, except for one minor issue: it copies value that is shown in the cell (Text
property), instead of real value contained in the cell (Value
property). Is there an option to copy real value, instead of shown value?
I've tried using
PerformAction(UltraGridAction.Copy, false, false);
and looking for some method or way to copy real values, but haven't found none. I've also tried to implement my own copy function, but this creates CSV data, and doesn't copy actual cells.
void OnExportToClipboardSelectedRows(object sender, EventArgs e)
{
List<UltraGridRow> rows = this.Grid.GetAllSelectedRows().ToList();
Console.WriteLine(rows[0].Cells.Count);
List<string> newRows = new List<string>();
if (rows.Count > 0)
{
int minRowIndex = -1;
int maxRowIndex = -1;
foreach (var row in rows)
{
if (row.Index < minRowIndex || minRowIndex == -1)
minRowIndex = row.Index;
if (row.Index > maxRowIndex || maxRowIndex == -1)
maxRowIndex = row.Index;
}
List<int> selectedCols = new List<int>();
foreach (var cell in this.Grid.Selected.Cells)
{
if (!selectedCols.Contains(cell.Column.Index))
selectedCols.Add(cell.Column.Index);
}
for (int i = minRowIndex; i <= maxRowIndex; i++)
{
List<string> cells = new List<string>();
foreach (int j in selectedCols)
{
cells.Add(this.Grid.Rows[i].Cells[j].Value.ToString());
}
newRows.Add(String.Join("\t", cells));
}
Clipboard.SetText(String.Join("\n", newRows));
}
else
{
MessageBox.Show("No selected rows found.");
}
}
After exhaustive trial/error attempts I finally come to working solution:
var selectedCells = this.Grid.Selected.Cells;
// Loop through selected cells and put them in "edit mode" (actually, show plain text)
foreach (var cell in selectedCells)
{
Console.WriteLine(cell.CellDisplayStyle);
cell.CellDisplayStyle = CellDisplayStyle.PlainText;
}
// Execute copy command
this.Grid.PerformAction(UltraGridAction.Copy, false, false);
// Loop through selected cells and bring them back to original state
foreach (var cell in selectedCells)
{
Console.WriteLine(cell.CellDisplayStyle);
cell.CellDisplayStyle = CellDisplayStyle.Default;
}
When CellDisplayStyle
is set to PlainText
, cell displays actual value, rather then formatted value. In that state, perform Copy
and return cells to original state.