当用户按下C#中DataGridView的最后一列时如何添加新行 [英] How to add new row when user press enters on last column of DataGridView in C#

查看:133
本文介绍了当用户按下C#中DataGridView的最后一列时如何添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 当用户在 DataGridView 的最后一列上按Enter键时,如何跳至下一行?

  • How do I jump to the next row when user presses enter on the last column of DataGridView?

此代码还给了我一个我需要解决的错误:

This code also gives me an error which I need to resolve:


索引为超出范围必须是非负数并且小于
集合的大小。

Index is out of range must be non-negative and less than the size of collection.



private void DataGridView1_KeyDown(object sender, KeyEventArgs e)
{
    try
    {
        if (e.KeyCode == Keys.Enter)
        {
            e.SuppressKeyPress = true;
            int iColumn = DataGridView1.CurrentCell.ColumnIndex;
            int iRow = DataGridView1.CurrentCell.RowIndex;
            if (iColumn >= DataGridView1.Columns.Count - 2)
                DataGridView1.CurrentCell = DataGridView1[0, iRow + 1];
            else
                DataGridView1.CurrentCell = DataGridView1[iColumn + 1, iRow];
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString())
    }
}


推荐答案

您可以使用 DataGridView.KeyDown 处理此问题,但是您会错过用户在编辑单元格时按 Enter 的情况。要捕获所有情况,请从 DataGridView 派生您自己的类,并覆盖 ProcessCmdKey 。使用上面注释中建议的逻辑,当在最后一行中检测到 Enter 时,添加另一行。不必担心手动更改 CurrentCell 。让基类自行处理。

You could handle this using DataGridView.KeyDown, but you would miss the case where the user hits Enter while editing a cell. To catch all cases, derive your own class from DataGridView and override ProcessCmdKey. Using the logic suggested in comments above, when Enter is detected for the last Row, add another row. Don't worry about manually changing the CurrentCell. Let the base class handle that work itself.

public class MyDataGridView : DataGridView
{
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        bool enterKeyDetected = keyData == Keys.Enter;
        bool isLastRow = this.CurrentCell.RowIndex == this.RowCount - 1;

        if (enterKeyDetected && isLastRow)
        {
            Console.WriteLine("Enter detected on {0},{1}", this.CurrentCell.RowIndex, this.CurrentCell.ColumnIndex);

            // Add the new row.
        }

        return base.ProcessCmdKey(ref msg, keyData);
    }
}

您只需要注意 how ,根据您的 DataSource 添加新行:

You need only pay attention to how you add the new row depending on your DataSource:


  • None

this.Rows.Add();


  • A DataTable

    DataTable dt = (DataTable)this.DataSource;
    dt.Rows.Add();
    


  • A BindingList< T> (请参见列表与绑定列表

    BindingList<T> bl= (BindingList<T>)this.DataSource;
    bl.Add(new T());
    


  • 等。

  • Etc.

    最后,只需将您的 DataGridView实例替换为此自定义实例即可。

    Lastly, just replace your instance of DataGridView with an instance of this custom one.

    所有这些,如果您没有 DataSource ,只需设置 DataGridView.AllowUserToAddRows = true 就足够了,因为编辑 NewRow 将触发另一个添加。

    All that said, if you don't have a DataSource, just setting DataGridView.AllowUserToAddRows = true should suffice since editing the NewRow will trigger another to be added.

    这篇关于当用户按下C#中DataGridView的最后一列时如何添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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