Get the value from DataGridView to TextBox on c#. TextBox event in DataGridView in c#.

using System;

using System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;

namespace GirdViewTextBoxEvent

{
public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

{

var txtBox = e.Control as TextBox;

if (txtBox != null)

{

txtBox.Leave += new EventHandler(txtBox_Leave);

}

}

void txtBox_Leave(object sender, EventArgs e)

{

try

{

for (int i = 0; i < dataGridView1.Rows.Count; i++)

{

int a = 0, b = 0, c = 0;

a = Convert.ToInt32(dataGridView1.Rows[i].Cells["txtRate"].Value);

b = Convert.ToInt32(dataGridView1.Rows[i].Cells["txtQuantity"].Value);

c = a * b;

dataGridView1.Rows[i].Cells["txtTotal"].Value = c;

}

}

catch (Exception ex)

{

MessageBox.Show(ex.Message.ToString());

}

}

}

}