DataGridView.GetClipboardContent 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
擷取代表選取儲存格內容的格式化值,以便複製到 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
傳回
A DataObject 代表所選儲存格的內容。
例外狀況
範例
以下程式碼範例示範如何以程式方式將選定 DataGridView 內容加入剪貼簿。 此範例是「 如何:啟用使用者從 Windows 表單 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.UnicodeText、 DataFormats.Html、 DataFormats.CommaSeparatedValue和 。
如需詳細資訊,請參閱 Clipboard 類別。
給繼承者的注意事項
覆寫此方法以提供自訂剪貼簿值。 例如,這對於支援從自訂儲存格類型複製值非常有用。