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

查看:39
本文介绍了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,然后选择第二个单元格,然后再次 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.

以下代码显示了用于将单元格设置为可编辑的 KeyDown 事件的 eventHandler.

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);               
        }
    }

如果之前没有找到,DataGridView FAQ 是一个很好的资源,由 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天全站免登陆