Code Heaven For C# Developer

Wednesday, September 29, 2010

working with DataGridViewCell Text Changed event...

Here i am going to solve a problem,, look this pics.....




For this first i take a DataGridView on my Form.Then i call Form load event.In this event i call dataGridView1.EditingControlShowing event of Grid.After this in this event i cast DataGridView cell into TextBox and then i call TextBox_textchanged event.so whole code will b like this...

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox tx = e.Control as TextBox;
DataGridViewTextBoxCell cell = (DataGridViewTextBoxCell)dataGridView1.CurrentCell;
if (tx != null && cell.OwningColumn == dataGridView1.Columns[1])
{

tx.TextChanged += new EventHandler(tx_TextChanged);

}
}

void tx_TextChanged(object sender, EventArgs e)
{
double rate;
TextBox tb = (TextBox)sender;
if (!string.IsNullOrEmpty(tb.Text))
rate = Convert.ToDouble(tb.Text);
else
rate = 0;
double qty = Convert.ToDouble(dataGridView1.CurrentRow.Cells[0].Value);
dataGridView1.CurrentRow.Cells[2].Value = qty * rate;
}
}

Now its working mast...........

Cheerssssssssss.................

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home