DataGridView.ClipboardCopyMode 속성

정의

사용자가 셀 텍스트 값을 Clipboard에 복사할 수 있는지 여부와 행 및 열 머리글 텍스트가 포함되어 있는지 여부를 나타내는 값을 가져오거나 설정합니다.

public:
 property System::Windows::Forms::DataGridViewClipboardCopyMode ClipboardCopyMode { System::Windows::Forms::DataGridViewClipboardCopyMode get(); void set(System::Windows::Forms::DataGridViewClipboardCopyMode value); };
[System.ComponentModel.Browsable(true)]
public System.Windows.Forms.DataGridViewClipboardCopyMode ClipboardCopyMode { get; set; }
[<System.ComponentModel.Browsable(true)>]
member this.ClipboardCopyMode : System.Windows.Forms.DataGridViewClipboardCopyMode with get, set
Public Property ClipboardCopyMode As DataGridViewClipboardCopyMode

속성 값

DataGridViewClipboardCopyMode 값 중 하나입니다. 기본값은 EnableWithAutoHeaderText입니다.

특성

예외

이 속성을 설정할 때 지정된 값이 유효한 DataGridViewClipboardCopyMode 값이 아닙니다.

예제

다음 코드 예제에서 복사를 사용 하도록 설정 하는 방법에 설명 합니다 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

설명

컨트롤은 DataGridView 선택한 각 셀의 텍스트 표현을 클립보드에 복사합니다. 이 값은 문자열로 변환된 셀 값이거나, 이미지 셀의 경우 속성 값입니다 Description .

값은 , , UnicodeTextHtmlCommaSeparatedValue 형식의 클립보드에 Text복사됩니다. 이러한 형식은 메모장, Microsoft Excel 및 Microsoft Word 같은 애플리케이션에 콘텐츠를 붙여넣는 데 유용합니다.

컨트롤은 DataGridView 선택한 모든 셀을 포함하는 가장 작은 사각형에 포함된 셀을 복사합니다. 선택한 셀을 포함하지 않는 행과 열은 복사된 데이터에 표시되지 않습니다. 나머지 행과 열의 선택되지 않은 셀은 빈 자리 표시자로 표시됩니다. 복사 모드 및 선택 모드에 따라 복사된 행 및 열의 헤더 값도 복사할 수 있습니다.

사용자가 콘텐츠를 복사할 때 컨트롤은 DataGridView 클립보드에 를 DataObject 추가합니다. 이 데이터 개체는 메서드에서 검색됩니다 GetClipboardContent . 프로그래밍 방식으로 클립보드에 데이터 개체를 추가하려는 경우 이 메서드를 호출할 수 있습니다.

메서드는 GetClipboardContent 메서드를 호출하여 개별 셀의 값을 검색합니다 DataGridViewCell.GetClipboardContent . 파생 클래스에서 이러한 메서드 중 하나 또는 둘 다를 재정의하여 복사된 셀의 레이아웃을 사용자 지정하거나 추가 데이터 형식을 지원할 수 있습니다.

클립보드 작업 및 데이터 형식에 대한 자세한 내용은 클래스를 Clipboard 참조하세요.

적용 대상

추가 정보