datagrid视图中的keypress事件? [英] keypress event in datagrid view?

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

问题描述

我需要写一个简单的功能,当人们输入框数,然后按键事件触发,并且框数* someamount 进入量列。我已经使用拖放控件添加了datagridview





我认为这里的代码将根据我的研究写在这里。

  private void dataGridView1_EditingControlShowing发件人,
DataGridViewEditingControlShowingEventArgs e){

}

但是不知道如何将Keyup事件和访问列 numberofboxes和Amount 。谢谢

解决方案

我已经测试了这个,它的工作原理是使用key down事件,并将 NumberBoxes value by someAmount ,每当您在单元格中输入新的号码时,它会自动计算。

  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);
//获取所选单元格的列和行位置
int column = dataGridView1.CurrentCellAddress.X;
int row = dataGridView1.CurrentCellAddress.Y;

if(column == 1)
{
//获取该单元格中存在的值
string test = dataGridView1 [column,row] .EditedFormattedValue。 ToString();
//将当前按键合并到单元格的内容
test = test + cellValue;
int intNumberBoxes = Convert.ToInt32(test);
//一些数量多个数字框
int someAmount = 10;
dataGridView1 [column + 1,row] .Value = intNumberBoxes * 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

I think so code will be written here according to my research

private void dataGridView1_EditingControlShowing(object sender,
    DataGridViewEditingControlShowingEventArgs e) {

}

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

解决方案

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


}

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

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