在数据网格视图按键事件? [英] keypress event in datagrid view?

查看:208
本文介绍了在数据网格视图按键事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个简单的函数,当人进入的盒子编号,然后按键事件触发,箱数* someamount 获得的金额列。我已经使用拖放控制

I need to write a simple function that when the person enter number of boxes then keypress event fired and number of boxes*someamount get in to the amount column. I have added datagridview using drag and drop control

我想根据我的研究

private void dataGridView1_EditingControlShowing(object sender,
    DataGridViewEditingControlShowingEventArgs e) {

}

但我不知道如何把KeyUp事件和接入列 numberofboxes和金额。谢谢

But i dont know how to put Keyup event and access column numberofboxes and Amount. Thanks

推荐答案

我测试过这一点,它的工作原理是使用键按下事件和倍数 NumberBoxes someAmount 值,每次它会自动计算你的单元格中输入一个新号码的时间。

I've tested this and it works by using the key down event and multiples the NumberBoxes value by someAmount, each time you enter a new number in the cell it does the calculation for you automatically.

        public Form1()
    {
        InitializeComponent();
        MyDataGridViewInitializationMethod();
    }


    private void MyDataGridViewInitializationMethod()
    {

        dataGridView1.EditingControlShowing +=
    new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
    }

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        e.Control.KeyPress +=
            new KeyPressEventHandler(Control_KeyPress);
    }

    private void Control_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (char.IsNumber(e.KeyChar))
        {

            string cellValue = Char.ToString(e.KeyChar);
            //Get the column and row position of the selected cell
            int column = dataGridView1.CurrentCellAddress.X;
            int row = dataGridView1.CurrentCellAddress.Y;

            if (column == 1)
            {
            //Gets the value that existings in that cell
            string test = dataGridView1[column, row].EditedFormattedValue.ToString();
            //combines current key press to the contents of the cell
            test = test + cellValue;
            int intNumberBoxes = Convert.ToInt32(test);
            //Some amount to mutiple the numberboxes by
            int someAmount = 10;
            dataGridView1[column + 1, row].Value = intNumberBoxes * someAmount;
            }
        }
    }


}

这篇关于在数据网格视图按键事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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