how to get a event if negative price in Column

K

Kim S

I have a Datagridview with a Price column ( in test string) how to get a
messagebox then the price is negative. I know how to make a messagebox it's
the event type in datagridview i don't know.

there should be a DataGirdViewCurrentBoxColumn type in datagridview
 
J

jp2msft

DataGridView.CurrentCell.OwningColumn will return a DataGridViewColumn.

DataGridViewColumn c = DataGridView1.CurrentRow.Selected;
string s = c.HeaderText;

Take a look at it to see if that gives you what you want.
 
F

Family Tree Mike

You should check the value in the CellValidating event, and pop up the
messagebox under the conditions you want. You may even want to set the
cancel flag in this condition.
 
K

Kim S

Its get posible to error Text so I try correct the value. I have try
cellEnter, CellLeave and RowEnter but the correcred result first get then I
move the courser back again

code:
private void NoNegativeValue_event(object sender, DataGridViewCellEventArgs
e)
{
decimal? tempValue = (decimal?)dataGridView["price",
e.RowIndex].Value;
if (tempValue < 0.00M || tempValue == null)
{
tempValue = -tempValue;
dataGridView["Price", e.RowIndex].Value = tempValue;

}
}


--
Kim S.


Family Tree Mike said:
You should check the value in the CellValidating event, and pop up the
messagebox under the conditions you want. You may even want to set the
cancel flag in this condition.
 
F

Family Tree Mike

If I understand you correctly you are checking the price value anytime a
cell change occurs, and possibly changing a cell not involved in the
CellLeave event. This does not make sense to me, but, that doesn't mean it
shouldn't work. The CellValidating event is designed for what you want to
do, to validate a cells new content. Have you tried the CellValidating
event?

Kim S said:
Its get posible to error Text so I try correct the value. I have try
cellEnter, CellLeave and RowEnter but the correcred result first get then
I
move the courser back again

code:
private void NoNegativeValue_event(object sender,
DataGridViewCellEventArgs
e)
{
decimal? tempValue = (decimal?)dataGridView["price",
e.RowIndex].Value;
if (tempValue < 0.00M || tempValue == null)
{
tempValue = -tempValue;
dataGridView["Price", e.RowIndex].Value = tempValue;

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top