How to get the formatted value of cell in GridView?

1 Answer 18 Views
GridView
Marian
Top achievements
Rank 2
Iron
Iron
Iron
Marian asked on 29 Apr 2024, 10:55 AM

Hello,
I have question about getting / copying formatted data in GridView. How can I get outside of GridView formatted value of cell? Second, how can I copy formatted data generally? I understand GridView uses virtualization and it formats data only for visual cells. But definitely there must be some methods for formatting data, that are at least used for formatting visual cells from underlying data. I have seen more forum threads and few samples in KB and documentation, I know there are Copying and CopyingCellClipboardContent events, but all samples demostrates manual formatting, for example lookup in combo box cell, or short time string for DateTime. I would like to get standard formatting, as it's displayed in GridView.

Maybe I should ask only to getting formatted value programatically, not standard Copy in GridView, but I have seen more samples for this. I was using WinForms DataGridView before, and there is FormattedValue property in cell object. I think there have to be a way how to get formatted value also in GridView, maybe by calling some method of GridView. About Copy, the best for me would be if I can get formatted value and do Copy manually, because I want to copy more data formats, formatted text representation and binary data for underlying data objects.

Btw. I was surprised GridView copies raw data and not formatted, I think standard and expected behavior for user is to copy what you can see, not some raw data.

1 Answer, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 02 May 2024, 08:14 AM

Hello Marian,

Thank you for reaching out to us.

To avoid any misunderstanding can you share how you have formatted the cell values? I have tested it with DateTime type and setting the FormatString property of the GridDateTimeColumn will format the cell value but it will not change the Value property of the cell. When I copy the cell, the visual cell text is copied. Is it possible to share more details about your set-up? How the cell values are formatted in your application?

In general, the Value property of the cell contains the value coming from the bound property. The Text property of the cell shows the visual text in the cell.

If you want to interfere in the coping operation, you can use the Copying event. Let's say the RadGridView is bound to a BindingList of custom objects. In the Copying event, you can cancel it and create your DataObject. Build your string from the bound collection (here you can apply your formatting) and pass the newly created DataObject to the Clipboard.SetDataObject() method.

private void RadGridView1_Copying(object sender, GridViewClipboardEventArgs e)
{
    if (e.Format == DataFormats.Text)
    {
        string result = string.Empty;
        var arr = myObjects.Select(x => x.Name).ToArray();
        result = string.Join(Environment.NewLine, arr);
        DataObject dataObject = new DataObject();
        dataObject.SetData(DataFormats.UnicodeText, false, result);
        dataObject.SetData(DataFormats.Text, false, result);
        Clipboard.SetDataObject(dataObject);
    }
    e.Cancel = true;
}

Regards,
Dinko | Tech Support Engineer
Progress Telerik

A brand new ThemeBuilder course was just added to the Virtual Classroom. The training course was designed to help you get started with ThemeBuilder for styling Telerik and Kendo UI components for your applications. You can check it out at https://learn.telerik.com
Tags
GridView
Asked by
Marian
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or