You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取GridView中鼠标选中/高亮的文本?

在GridView中获取鼠标高亮选中的文本

我想知道当用鼠标在GridView里高亮/标记文本时,能不能获取到这些选中的文本?希望点击按钮时把它保存到字符串变量里,不管通过GridView还是其他方式,只要能拿到标记的文本就行。

我试过点击按钮时触发Ctrl+C把文本存到剪贴板,但用System.Windows.Forms时出了问题,也没找到类似gridview.GetSelectedText的事件。

我已经写了一个控制器,先想获取GridView中选中的文本:

namespace XPress.Module.Controllers
{
    public class TransactionAddStringToCategoryKeywordController : ObjectViewController<ObjectView, Transaction>
    {
        private const string addStringToCategoryKeywordId = "AddStringToCategoryKeyword";
        private readonly SimpleAction addStringToCategoryKeyword;

        public TransactionAddStringToCategoryKeywordController()
        {
            addStringToCategoryKeyword = new SimpleAction(this, addStringToCategoryKeywordId, DevExpress.Persistent.Base.PredefinedCategory.Edit);
            addStringToCategoryKeyword.ImageName = "Action_Export";

            //addStringToCategoryKeyword.TargetObjectsCriteria = "Not IsNullOrEmpty([NameFull])";
            addStringToCategoryKeyword.SelectionDependencyType = SelectionDependencyType.RequireMultipleObjects;
            addStringToCategoryKeyword.Execute += TransactionAddStringToCategoryKeyword_Execute;
        }

        protected override void OnActivated()
        {
            base.OnActivated();

            addStringToCategoryKeyword.Active["test"] = true;
        }
    }
}

而且我已经能访问到GridView:

private void WinAlternatingRowsController_ViewControlsCreated(object sender, EventArgs e)
{
    GridListEditor listEditor = ((ListView)View).Editor as GridListEditor;

    if (listEditor != null)
    {
        DevExpress.XtraGrid.Views.Grid.GridView gridView = listEditor.GridView;
    }
}

解决方案

可以通过DevExpress GridView的GetSelectedText方法直接获取选中的文本,以下是具体实现步骤:

  1. 在控制器中存储GridView引用
    重写控制器的OnViewControlsCreated方法,获取并保存GridView实例:

    private DevExpress.XtraGrid.Views.Grid.GridView _gridView;
    
    protected override void OnViewControlsCreated()
    {
        base.OnViewControlsCreated();
        GridListEditor listEditor = View.Editor as GridListEditor;
        if (listEditor != null)
        {
            _gridView = listEditor.GridView;
        }
    }
    
  2. 按钮点击时获取选中文本
    在按钮的Execute事件方法中,调用GetSelectedText获取文本:

    private void TransactionAddStringToCategoryKeyword_Execute(object sender, SimpleActionExecuteEventArgs e)
    {
        if (_gridView != null)
        {
            string selectedText = _gridView.GetSelectedText();
            // 将selectedText存入变量或执行后续业务逻辑
            if (!string.IsNullOrEmpty(selectedText))
            {
                // 示例:输出到调试窗口,替换为你的实际操作
                System.Diagnostics.Debug.WriteLine("选中的文本:" + selectedText);
            }
        }
    }
    
  3. 剪贴板方案的修复(可选)
    如果坚持用剪贴板方案,可调用GridView内置的复制方法后读取:

    private void TransactionAddStringToCategoryKeyword_Execute(object sender, SimpleActionExecuteEventArgs e)
    {
        if (_gridView != null)
        {
            _gridView.CopyToClipboard();
            string selectedText = System.Windows.Forms.Clipboard.GetText();
            // 后续处理逻辑
        }
    }
    

    注意:使用剪贴板需确保应用有访问权限,多线程场景需额外处理。


内容的提问来源于stack exchange,提问作者Sax

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 11:30:55