Datagridview:如何在编辑模式下设置单元格? [英] Datagridview: How to set a cell in editing mode?

查看:220
本文介绍了Datagridview:如何在编辑模式下设置单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以编程方式设置单元格。我知道将该单元格设置为CurrentCell,然后调用BeginEdit(bool)方法,它应该会发生,但在我的情况下,它不会。

I need to programmatically set a cell in editing mode. I know that setting that cell as CurrentCell and then call the method BeginEdit(bool), it should happen, but in my case, it doesn't.

我真的很想那个,我的DGV有几列,用户只能选择并编辑前两个。其他列已经是只读的,但用户可以选择它们,这就是我不想要的。

I really want that, with my DGV with several columns, the user can ONLY select and also edit the first two. The other columns are already read-only, but the user can select them, and that is what I don't want.

所以我在想,告诉用户TAB每当它完成在单元格上的写入,然后选择第二个单元格,然后选项卡再次选择并开始编辑下一行的第一个单元格...

So I was thinking, tell the user to TAB everytime it has finished writing on the cell, then select the second cell, then tab again and it select and begin edit the next row's first cell...

如何我这样做?

推荐答案

设置 CurrentCell 然后调用 BeginEdit(true)对我来说很好。

Setting the CurrentCell and then calling BeginEdit(true) works well for me.

以下代码显示了一个eventHandler,用于 KeyDown 事件,将单元格设置为可编辑。

The following code shows an eventHandler for the KeyDown event that sets a cell to be editable.

我的示例只实现了所需的按键重写之一,但理论上其他应该是一样的。 (我总是将[0] [0]单元格设置为可编辑,但任何其他单元格都可以工作)

My example only implements one of the required key press overrides but in theory the others should work the same. (and I'm always setting the [0][0] cell to be editable but any other cell should work)

    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Tab && dataGridView1.CurrentCell.ColumnIndex == 1)
        {
            e.Handled = true;
            DataGridViewCell cell = dataGridView1.Rows[0].Cells[0];
            dataGridView1.CurrentCell = cell;
            dataGridView1.BeginEdit(true);               
        }
    }

如果您以前没有找到它, a href =https://www.google.de/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CCwQFjAA&url=http%3A% 2F%2Fdownload.microsoft.com%2Fdownload%2F5 2F6%%2F4%2F5646742C-3EB7-48F7-BFB3-CC295D618CF9%2FDataGridView%2520FAQ.doc&安培; EI = FFgTUrW2HYfChAeMnYDwCg&安培; USG = AFQjCNHE0C79uGnD14VrXWm07wvfhzPWUQ&安培; BVM = bv.50952593,d.ZG4\" rel =noreferrer> DataGridView常见问题是一个伟大的资源,由程序管理员为DataGridView控件编写,它涵盖了大部分您可能想要做的控件。

If you haven't found it previously, the DataGridView FAQ is a great resource, written by the program manager for the DataGridView control, which covers most of what you could want to do with the control.

这篇关于Datagridview:如何在编辑模式下设置单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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