
3 out of 9 rated this helpful - Rate this topic

DataObject Class

Implements a basic data transfer mechanism.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
public class DataObject : IDataObject, IDataObject

DataObject implements the IDataObject interface, whose methods provide a format-independent mechanism for data transfer.

A DataObject is typically used with the Clipboard and in drag-and-drop operations. The DataObject class provides the recommended implementation of the IDataObject interface. It is suggested that you use the DataObject class rather than implementing IDataObject yourself.

Multiple pieces of data in different formats can be stored in a DataObject. Data is retrieved from a DataObject by its associated format. Because the target application might not be known, you can increase the likelihood that the data will be in the appropriate format for an application by placing the data in a DataObject in multiple formats. See DataFormats for the predefined formats. You can implement your own format by creating an instance of the DataFormats.Format class.

To store data in a DataObject, pass the data to the constructor or call SetData. You can add data in multiple formats to the same DataObject. If you want the data you add to be retrieved in its native format only, call SetData(String, Boolean, Object) with the autoConvert parameter set to false.

Data can be retrieved from a DataObject in any format which is compatible with GetData. For example, text can be converted to Unicode. To retrieve data in the format in which it was stored, call GetData with the autoConvert parameter set to false.

To determine what formats the data is stored in, call GetFormats. To determine if a format is available, call GetDataPresent with the desired format.

In .NET Framework version 2.0, the DataObject class provides additional methods that make it easier to work with data in common formats. To add data of a particular format to the DataObject, use the appropriate SetFormat method, such as SetText. To retrieve data of a particular format from the DataObject, first call the appropriate ContainsFormat method (such as ContainsText) to determine whether the DataObject contains data in that format, then call the appropriate GetFormat method (such as GetText) to retrieve the data if the DataObject contains it.



An object must be serializable for it to be put on the Clipboard. See System.Runtime.Serialization for more information on serialization. If your target application requires a very specific data format, the headers added to the data in the serialization process may prevent the application from recognizing your data. To preserve your data format, add your data as a Byte array to a MemoryStream and pass the MemoryStream to the SetData method.

The following code example adds data in a DataObject. First, a new DataObject is created and a component is stored in it. Then, it checks to see if data in the appropriate type exists in the DataObject. The result is displayed in a text box. This code requires that textBox1 has been created.

private void AddMyData3() {
    // Creates a component to store in the data object.
    Component myComponent = new Component();

    // Creates a new data object.
    DataObject myDataObject = new DataObject();

    // Adds the component to the DataObject.

    // Prints whether data of the specified type is in the DataObject.
    Type myType = myComponent.GetType();
       textBox1.Text = "Data of type " + myType.ToString() + 
       " is present in the DataObject";
       " is not present in the DataObject";

The next example retrieves the data stored in a DataObject. First, a new DataObject is created with text data. Then, the data is retrieved, specifying its format as a string, and displayed in a text box. The data format is automatically converted from text to string. This code requires that textBox1 has been created.

private void GetMyData2() {
    // Creates a new data object using a string and the text format.
    DataObject myDataObject = new DataObject(DataFormats.Text, "Text to Store");

    // Prints the string in a text box.
    textBox1.Text = myDataObject.GetData(DataFormats.Text).ToString();



.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0
Did you find this helpful?
(1500 characters remaining)

Community Additions

© 2013 Microsoft. All rights reserved.