DataGridView-“单元格选择样式”; -编辑单元格 [英] DataGridView - "Cell Selection Style" - Edit Cell

查看:108
本文介绍了DataGridView-“单元格选择样式”; -编辑单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有DataGridView控件的WinForm客户端。我注意到用户必须单击一次以选择该单元格,然后再次对其进行编辑。将其更改为单击编辑模式的方法是什么?我以为以前看过类似的东西,但是记不起来了。

I'm working on a WinForm client with a DataGridView control. I notice users have to click once to select the cell and again to edit it. What is the way to change this to a single click edit mode? I thought I had seen something like this before but can't remember the name.

推荐答案

好了,我注意到 EditMode.EditOnEnter

它会使许多DataGriView的默认行为产生偏差,这很烦人。
除其他外,即使显式调用EndEdit方法(您被迫单击另一个控件以使datagridview单元失去焦点),编辑后的单元仍保持处于编辑模式。

Well I have noticed a problem with EditMode.EditOnEnter
It biases a lot of DataGriView's default behavior, which is irritating. Among others, the edited cell stays in edited mode even when the EndEdit method is explicitly called (you are force to click on another control to make the datagridview cell lose its focus.)

下面的代码效果很好,它使您可以通过单击任意单元格进行编辑,然后通过按Enter或在DGView外部单击来结束编辑(就像您在默认行为中所做的那样)

The following piece of code works pretty fine as it makes you edit by single clicking on any cell and ending the edit by hitting enter or clicking outside the DGView (just as you would do in the default behavior)

在这里:

    private void myDatagridView_MouseUp(object sender, MouseEventArgs e)
    { 
        if (e.Button == MouseButtons.Left)
        {
            hitTestInfo = myDatagridView.HitTest(e.X, e.Y);
            if (hitTestInfo.Type == DataGridViewHitTestType.Cell) 
                myDatagridView.BeginEdit(true);
            else 
                myDatagridView.EndEdit();
        }
    } 

这篇关于DataGridView-“单元格选择样式”; -编辑单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆