DataGridView.GetClipboardContent 메서드

정의

Clipboard에 복사할 선택된 셀 내용을 나타내는 서식 있는 값을 검색합니다.

public:
 virtual System::Windows::Forms::DataObject ^ GetClipboardContent();
public virtual System.Windows.Forms.DataObject GetClipboardContent ();
public virtual System.Windows.Forms.DataObject? GetClipboardContent ();
abstract member GetClipboardContent : unit -> System.Windows.Forms.DataObject
override this.GetClipboardContent : unit -> System.Windows.Forms.DataObject
Public Overridable Function GetClipboardContent () As DataObject

반환

선택한 셀의 내용을 나타내는 DataObject입니다.

예외

예제

다음 코드 예제에서는 선택한 DataGridView 콘텐츠를 클립보드에 프로그래밍 방식으로 추가하는 방법을 보여 줍니다. 이 예제는 방법: 사용자가 Windows Forms DataGridView 컨트롤에서 클립보드에 여러 셀을 복사할 수 있도록 설정에서 사용할 수 있는 더 큰 예제의 일부입니다.

private void Form1_Load(object sender, System.EventArgs e)
{
    // Initialize the DataGridView control.
    this.DataGridView1.ColumnCount = 5;
    this.DataGridView1.Rows.Add(new string[] { "A", "B", "C", "D", "E" });
    this.DataGridView1.Rows.Add(new string[] { "F", "G", "H", "I", "J" });
    this.DataGridView1.Rows.Add(new string[] { "K", "L", "M", "N", "O" });
    this.DataGridView1.Rows.Add(new string[] { "P", "Q", "R", "S", "T" });
    this.DataGridView1.Rows.Add(new string[] { "U", "V", "W", "X", "Y" });
    this.DataGridView1.AutoResizeColumns();
    this.DataGridView1.ClipboardCopyMode = 
        DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
}

private void CopyPasteButton_Click(object sender, System.EventArgs e)
{
    if (this.DataGridView1
        .GetCellCount(DataGridViewElementStates.Selected) > 0)
    {
        try
        {
            // Add the selection to the clipboard.
            Clipboard.SetDataObject(
                this.DataGridView1.GetClipboardContent());
            
            // Replace the text box contents with the clipboard text.
            this.TextBox1.Text = Clipboard.GetText();
        }
        catch (System.Runtime.InteropServices.ExternalException)
        {
            this.TextBox1.Text = 
                "The Clipboard could not be accessed. Please try again.";
        }
    }
}
Private Sub Form1_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Me.Load

    ' Initialize the DataGridView control.
    Me.DataGridView1.ColumnCount = 5
    Me.DataGridView1.Rows.Add(New String() {"A", "B", "C", "D", "E"})
    Me.DataGridView1.Rows.Add(New String() {"F", "G", "H", "I", "J"})
    Me.DataGridView1.Rows.Add(New String() {"K", "L", "M", "N", "O"})
    Me.DataGridView1.Rows.Add(New String() {"P", "Q", "R", "S", "T"})
    Me.DataGridView1.Rows.Add(New String() {"U", "V", "W", "X", "Y"})
    Me.DataGridView1.AutoResizeColumns()
    Me.DataGridView1.ClipboardCopyMode = _
        DataGridViewClipboardCopyMode.EnableWithoutHeaderText

End Sub

Private Sub CopyPasteButton_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles CopyPasteButton.Click

    If Me.DataGridView1.GetCellCount( _
        DataGridViewElementStates.Selected) > 0 Then

        Try

            ' Add the selection to the clipboard.
            Clipboard.SetDataObject( _
                Me.DataGridView1.GetClipboardContent())

            ' Replace the text box contents with the clipboard text.
            Me.TextBox1.Text = Clipboard.GetText()

        Catch ex As System.Runtime.InteropServices.ExternalException
            Me.TextBox1.Text = _
                "The Clipboard could not be accessed. Please try again."
        End Try

    End If

End Sub

설명

이 메서드는 선택한 셀에서 정의한 영역을 나타내는 데이터를 검색합니다. 이 영역은 선택한 모든 셀을 포함하는 가장 작은 사각형입니다. 이 지역에서 선택한 각 셀의 값은 메서드를 호출 DataGridViewCell.GetClipboardContent 하여 검색됩니다. 빈 자리 표시자 값은 이 영역의 선택되지 않은 셀에 사용됩니다. 이 메서드는 이러한 값을 클립보드에 DataObject 복사하기 위한 여러 형식을 포함하는 로 결합합니다. 지원 되는 클립보드 형식 포함 DataFormats.Text, DataFormats.UnicodeTextDataFormats.Html, 및 DataFormats.CommaSeparatedValue합니다.

자세한 내용은 Clipboard 클래스를 참조하세요.

상속자 참고

사용자 지정된 클립보드 값을 제공하려면 이 메서드를 재정의합니다. 예를 들어 사용자 지정 셀 형식의 값 복사를 지원하는 데 유용합니다.

적용 대상

추가 정보