简体   繁体   English

手动移动到DataGridView中的下一个单元格?

[英]Manually move to next cell in DataGridView?

I have a DataGridView and I have 2 editable cells. 我有一个DataGridView,我有2个可编辑单元格。 The first cell uses a custom control and the second is a standard textbox. 第一个单元格使用自定义控件,第二个单元格是标准文本框。

The custom control has an ItemChanged event and this event is raised when the user enters a value in the control. 定制控件具有ItemChanged事件,并且当用户在控件中输入值时会引发此事件。

How can I subscribe to this event and how can I move to the next editable cell? 如何订阅此活动,以及如何移至下一个可编辑单元格?

As per this answer : 按照这个答案

private void customControl_ItemChanged(object sender, KeyEventArgs e)
    {
            int iColumn = dataGridView1.CurrentCell.ColumnIndex;
            int iRow = dataGridView1.CurrentCell.RowIndex;
            if (iColumn == dataGridView1.Columns.Count-1)
                dataGridView1.CurrentCell = dataGridView1[0, iRow + 1];
            else
                dataGridView1.CurrentCell = dataGridView1[iColumn + 1, iRow];
    }

You'll also need to add a line to the InitializeComponent() method in FormName.Designer.cs if you can't link the event via the designer, something like: 如果您无法通过设计器链接事件,则还需要在FormName.Designer.cs中的InitializeComponent()方法中添加一行,例如:

this.customControl.ItemChanged += new System.EventHandler(this.customControl_ItemChanged);

You can access the custom control in the DataGridView 's EditingControlShowing event by using e.Control . 您可以使用e.ControlDataGridViewEditingControlShowing事件中e.Control Here an exemple with a DataGridViewTextBoxCell : 这是一个带有DataGridViewTextBoxCell

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (e.Control is TextBox)
    {
        ((TextBox)e.Control).TextChanged += TextBoxCell_TextChanged;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM