簡體   English   中英

具有復制/粘貼功能的只讀DataGridView?

[英]Read-only DataGridView with Copy/Paste-Function?

我正在開發Winform應用程序,並使用Datagridview顯示記錄。

現在,我要求單元格內容應為只讀,但同時我希望最終用戶可以將單元格內容復制到剪貼板,而不要對其進行編輯。

請提出您是否有解決方案。

DataGridView.ReadOnly = True;   
DataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;

然后用戶可以從單元格中選擇數據(字符串),然后按CTRL + V將其放入剪貼板

您還可以將ContextMenu添加到DataGrid,並添加“復制和粘貼”項,並向其中添加Ctrl + V和Ctrl + C快捷鍵

然后,您處理他們的事件。 如果需要,可以將菜單設置為Visible = false,並且右鍵單擊時不會顯示菜單,但是快捷方式仍然可以使用。

就個人而言,我喜歡右鍵單擊菜單添加到DataGrid的觸摸,但這就是您的選擇。

您可以嘗試使用此代碼-基於DataGridViewCell.OnKeyPress

鏈接: http//msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.onkeypress.aspx

private void DataGridViewCell_KeyPress(KeyPressEventArgs e, int index)
{
     if ( (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl) 
          && e.Key == Key.C)
     {
        //You can get your cell based on index
        Clipboard.SetText(.....);
     }
}

您可以添加到XAML:ClipboardCopyMode =“ IncludeHeader” /“ ExcludeHeader” /“ NONE”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM