How to get the formatted value of cell in GridView?

1 Answer 19 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
Marian
Top achievements
Rank 2
Iron
Iron
Iron
commented on 16 May 2024, 10:32 PM

Hello,
first, this is the screenshot of my test program (I have also attached it, project TelerikTestReal in solution).


I have two types of formatting, first formatting is by FormatString property of column, R/Fi/Min/Max columns have FormatString set to {0:F3}. There is also some formatting in CellFormatting event for hiding some cells and special formatting for replacing Test Type with subitem name for subitem rows.

I know Value property contains "raw" data from bound property, but there is no Text property in GridViewCellInfo object. So the first question was how to get formatted value of cell? I know GridView uses virtualization and it formats data only for visual cells, but is there any way to format value programatically?

		private void tsBtnTest_Click(object sender, EventArgs e)
		{
			GridViewCellInfo cellMin = radGridView1.Rows[3].Cells["colMin"];
			var val = cellMin.Value;
			//var text = cellMin.Text;
			GridViewCellInfo cellSub = radGridView1.Rows[8].Cells["colTestType"];
			var valSub = cellSub.Value;
		}

About copy, when I copy (by ctrl+C or context menu) some cells, I get raw data to clipboard. For example, if you copy cell in second row in Min column and paste it to notepad, you get '10,2', not '10,200' as it's formatted by FormatString property, or if you copy cell in Test Type column in row next to P7.2, it copies null, not 'Vyska' as it's formatted by CellFormatting event. Generally, I want to copy data as it's displayed in GridView, not the raw data from bound object properties. And about Copying event, I don't want to have own formatting, I would like to get output of standard formatting procedure, as it displayed in GridView.

But maybe we should discuss getting formatted value programatically first and not deal with copying.

Tags
GridView
Asked by
Marian
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or