关于datagrid视图中的c#:keypress事件?

keypress event in datagrid view?

我需要编写一个简单的函数,当用户输入框数时,将触发键事件并number of boxes*someamount进入"金额"列。我已经使用拖放控件添加了datagridview

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

1
2
3
4
private void dataGridView1_EditingControlShowing(object sender,
    DataGridViewEditingControlShowingEventArgs e) {

}

但是我不知道如何放置Keyup事件和访问列numberofboxes and Amount。谢谢


我已经对此进行了测试,它通过使用按键事件并将NumberBoxes值乘以someAmount进行工作,每次在单元格中输入新数字时,它都会自动为您进行计算。 >

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
        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;
            }
        }
    }


}


对于vb.net

十进制验证:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 Public Sub New()
        InitializeComponent()
        MyDataGridViewInitializationMethod()
    End Sub
    Private Sub MyDataGridViewInitializationMethod()
        AddHandler dataGridView1.EditingControlShowing, AddressOf dataGridView1_EditingControlShowing
    End Sub
    Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs)
        AddHandler e.Control.KeyPress, AddressOf Control_KeyPress
    End Sub
    Dim dotOnce As Boolean
    Private Sub Control_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        If e.KeyChar Like"[']" Then
            e.Handled = True
            Exit Sub
        End If
        If e.KeyChar ="." Then
            If dotOnce Then
                e.Handled = True
            End If
            dotOnce = True
        Else
           If (Not e.KeyChar Like"[0-9 . ]") Then
                e.Handled = True
                Exit Sub
            End If
        End If
          End Sub