Abhishek
Abhishek

Reputation: 299

which control is clicked in datagridview

I want to perform an action in datagridview, like calculation. When the user type Amount in textbox, I want to calculate its instalment. The problem is that I have also a combobox in my datagridview. When I select something from the grid combobox, I get exception in my code, so I want to stop to perform my calculation when user click on combobox.
How can I know if the user have clicked or select something from combobox?

private void prol04DataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
     TextBox tx = e.Control as TextBox;
     // Below line i am geting error Because i select combobox in datagrid
     DataGridViewTextBoxCell cell = DataGridViewTextBoxCell)prol04DataGridView.CurrentCell;
     if (tx != null && cell.OwningColumn == prol04DataGridView.Columns[5])
     {
         tx.TextChanged -= new EventHandler(tx_TextChanged);
         tx.TextChanged += new EventHandler(tx_TextChanged);
     }             
}

So how can I find on which control on datagrid have the user performed an action?

Upvotes: 1

Views: 276

Answers (1)

Steve
Steve

Reputation: 216243

Apply the same logic used to cast the e.Control to a TextBox also to the CurrentCell

 TextBox tx = e.Control as TextBox;
 DataGridViewTextBoxCell cell = prol04DataGridView.CurrentCell as DataGridViewTextBoxCell;
 if (tx != null && cell != null && cell.OwningColumn == prol04DataGridView.Columns[5])
 {
       tx.TextChanged -= new EventHandler(tx_TextChanged);
       tx.TextChanged += new EventHandler(tx_TextChanged);

 }

Upvotes: 3

Related Questions