Reputation: 3412
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
Upvotes: 2
Views: 14368
Reputation: 199
for vb.net
Decimal validation :
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
Upvotes: 0
Reputation: 1517
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;
}
}
}
}
Upvotes: 7