DataObject.GetData Method

Definition

Returns the data associated with the specified data format.

Overloads

GetData(String)
Obsolete.

Returns the data associated with the specified data format.

GetData(Type)
Obsolete.

Returns the data associated with the specified class type format.

GetData(String, Boolean)
Obsolete.

Returns the data associated with the specified data format, using an automated conversion parameter to determine whether to convert the data to the format.

GetData(String)

Source:
DataObject.cs
Source:
DataObject.cs
Source:
DataObject.cs

Caution

DataObject.GetData methods are obsolete. Use the corresponding DataObject.TryGetData<T> instead.

Returns the data associated with the specified data format.

C#
public virtual object GetData(string format);
C#
[System.Obsolete("`DataObject.GetData` methods are obsolete. Use the corresponding `DataObject.TryGetData<T>` instead.", false, DiagnosticId="WFDEV005", UrlFormat="https://aka.ms/winforms-warnings/{0}")]
public virtual object? GetData(string format);
C#
public virtual object? GetData(string format);

Parameters

format
String

The format of the data to retrieve. See DataFormats for predefined formats.

Returns

The data associated with the specified format, or null.

Implements

Attributes

Examples

The following code 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.

This code requires that textBox1 has been created.

C#
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.
    myDataObject.SetData(myComponent);
 
    // Prints whether data of the specified type is in the DataObject.
    Type myType = myComponent.GetType();
    if(myDataObject.GetDataPresent(myType))
       textBox1.Text = "Data of type " + myType.ToString() + 
       " is present in the DataObject";
    else
       textBox1.Text = "Data of type " + myType.ToString() +
       " is not present in the DataObject";
 }

Remarks

If this method cannot find data in the specified format, it attempts to convert the data to the format. If the data cannot be converted to the specified format, or if the data was stored with automatic conversion set to false, this method returns null.

To determine whether data is associated with, or can be converted to, a format, call GetDataPresent before calling GetData. Call GetFormats for a list of valid formats for the data stored in this DataObject.

Note

Data can be converted to another format if it was stored specifying that conversion is allowed, and if the requested format is compatible with the stored format. For example, data stored as Unicode can be converted to text.

When format is Html, this method returns a UTF-8 encoded string in applications that target .NET 4.5 or higher, and an ANSI encoded string in applications that target .NET 4.0 or lower.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions (Obsolete)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9 (10)

GetData(Type)

Source:
DataObject.cs
Source:
DataObject.cs
Source:
DataObject.cs

Caution

DataObject.GetData methods are obsolete. Use the corresponding DataObject.TryGetData&lt;T&gt; instead.

Returns the data associated with the specified class type format.

C#
public virtual object GetData(Type format);
C#
[System.Obsolete("`DataObject.GetData` methods are obsolete. Use the corresponding `DataObject.TryGetData<T>` instead.", false, DiagnosticId="WFDEV005", UrlFormat="https://aka.ms/winforms-warnings/{0}")]
public virtual object? GetData(Type format);
C#
public virtual object? GetData(Type format);

Parameters

format
Type

A Type representing the format of the data to retrieve.

Returns

The data associated with the specified format, or null.

Implements

Attributes

Examples

The following code example retrieves the data stored in a DataObject. First, a new DataObject is created with a component. Then, the data is retrieved, specifying its type. The type of the retrieved data is displayed in a text box.

This code requires that textBox1 has been created.

C#
private void GetMyData() {
    // Creates a component to store in the data object.
    Component myComponent = new Component();
 
    // Creates a new data object and assigns it the component.
    DataObject myDataObject = new DataObject(myComponent);
 
    // Creates a type to store the type of data.
    Type myType = myComponent.GetType();
 
    // Retrieves the data using myType to represent its type.
    Object myObject = myDataObject.GetData(myType);
    if(myObject != null)
       textBox1.Text = "The data type stored in the DataObject is: " +
       myObject.GetType().Name;
    else
       textBox1.Text = "Data of the specified type was not stored " +
       "in the DataObject.";
 }

Remarks

If this method cannot find data in the specified format, it attempts to convert the data to the format. If the data cannot be converted to the specified format, or if the data was stored with automatic conversion set to false, this method returns null.

To determine whether data is associated with, or can be converted to, a format, call GetDataPresent before calling GetData. Call GetFormats for a list of valid formats for the data stored in this DataObject.

Note

Data can be converted to another format if it was stored specifying that conversion is allowed, and if the requested format is compatible with the stored format. For example, data stored as Unicode can be converted to text.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions (Obsolete)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9 (10)

GetData(String, Boolean)

Source:
DataObject.cs
Source:
DataObject.cs
Source:
DataObject.cs

Caution

DataObject.GetData methods are obsolete. Use the corresponding DataObject.TryGetData&lt;T&gt; instead.

Returns the data associated with the specified data format, using an automated conversion parameter to determine whether to convert the data to the format.

C#
public virtual object GetData(string format, bool autoConvert);
C#
[System.Obsolete("`DataObject.GetData` methods are obsolete. Use the corresponding `DataObject.TryGetData<T>` instead.", false, DiagnosticId="WFDEV005", UrlFormat="https://aka.ms/winforms-warnings/{0}")]
public virtual object? GetData(string format, bool autoConvert);
C#
public virtual object? GetData(string format, bool autoConvert);

Parameters

format
String

The format of the data to retrieve. See DataFormats for predefined formats.

autoConvert
Boolean

true to the convert data to the specified format; otherwise, false.

Returns

The data associated with the specified format, or null.

Implements

Attributes

Examples

The following code example retrieves the data stored in a DataObject, using the autoConvert parameter to specify whether to convert the data format.

First, a new DataObject is created with text data. Then the example tries to retrieve the data, specifying its format as a string and no format conversion, that is, the autoConvert parameter is false. This operation fails because there is no string data in the DataObject.

Next, the example tries to retrieve the data again, with the autoConvert parameter set to true. This operation succeeds and the results are displayed in a MessageBox.

This code requires that textBox1 has been created.

C#
private void GetMyData3() {
    // Creates a new data object using a string and the text format.
    string myString = "My new text string";
    DataObject myDataObject = new DataObject(DataFormats.Text, myString);
 
    // Prints the string in a text box with autoconvert = false.
    if(myDataObject.GetData("System.String", false) != null) {
       // Prints the string in a text box.
       textBox1.Text = myDataObject.GetData("System.String", false).ToString() + '\n';
    } else
        {
            textBox1.Text = "Could not find data of the specified format" + '\n';
        }

        // Prints the string in a text box with autoconvert = true.
        textBox1.Text += myDataObject.GetData("System.String", true).ToString();
 }

Remarks

If the autoConvert parameter is true and this method cannot find data in the specified format, it attempts to convert the data to the format. If the data cannot be converted to the specified format, or if the data was stored with the automatic conversion set to false, this method returns null.

If the autoConvert parameter is false, this method returns data in the specified format, or null if no data in this format can be found.

To determine whether data is associated with, or can be converted to, a format, call GetDataPresent before calling GetData. Call GetFormats for a list of valid formats for the data stored in this DataObject.

Note

Data can be converted to another format if it was stored specifying that conversion is allowed, and if the requested format is compatible with the stored format. For example, data stored as Unicode can be converted to text.

When format is Html, this method returns a UTF-8 encoded string in applications that target .NET 4.5 or higher, and an ANSI encoded string in applications that target .NET 4.0 or lower.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions (Obsolete)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9 (10)