Well, you seem to be on the right track by adding event handlers to the control that is used for editing. Ok, lets walk through it, you may need to change the variable names and function names to work in your project:
1. You have two events that you want to control: KeyPress and TextChanged.
private void dataGridView1_EditingControlShowing ( object sender, DataGridViewEditingControlShowingEventArgs e )
{
if ( this.dataGridView1.CurrentCell.ColumnIndex == 1 )
{
e.Control.TextChanged += new EventHandler ( Control_TextChanged );
e.Control.KeyPress += new KeyPressEventHandler ( Control_KeyPress );
}
}
2. Next, put the code in to control the keys used to force numeric value. You only need to address the situations that you do not want to process the keys for.
void Control_KeyPress ( object sender, KeyPressEventArgs e )
{
if ( "0123456789".IndexOf ( e.KeyChar ) == -1 && ".".IndexOf ( e.KeyChar ) == -1 && "\b".IndexOf ( e.KeyChar ) == -1 )
{
e.Handled = true;
}
}
3. Now, put the code in to control what happens when the text changes. The sender here is the TextBox being used to edit the DataGridViewCell.
void Control_TextChanged ( object sender, EventArgs e )
{
TextBox tbx = ( TextBox )sender;
List<double> templist = new List<double> ();
double temp;
foreach ( DataGridViewRow row in this.dataGridView1.Rows )
{
temp = 0;
if ( this.dataGridView1.CurrentCellAddress.Y == this.dataGridView1.Rows.IndexOf ( row ) )
{
if ( double.TryParse ( tbx.Text.ToString (), out temp ) )
{
templist.Add ( temp );
}
}
else
{
if ( double.TryParse ( row.Cells[1].Value.ToString (), out temp ) )
{
templist.Add ( temp );
}
}
}
this.textBox1.Text = templist.Sum ().ToString ();
}
Let me know if you have any questions about this.