Per chi dovesse gestire la stessa situazione, ho optato per un la gestione della
formattazione nell'evento LeaveCell.
codice:
// Nella InitializeComponent del Form :
// per il DataGridView grdWPreventivo
this.grdWPreventivo.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.GrdWPreventivoCellLeave);
// La procedura GrdWPreventivoCellLeave
void GrdWPreventivoCellLeave(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
string valore;
try {
if (e.ColumnIndex > 0) {
valore = this.grdWPreventivo[e.ColumnIndex, e.RowIndex].Value.ToString();
this.grdWPreventivo[e.ColumnIndex, e.RowIndex].Value = FormatRetValue(valore,2);
}
} catch {
return;
}
}
// La funzione FormatRetValue
public static string FormatRetValue(String valore, int n) {
// -------------------------------------------------------
// Valore formattato alle n cifre decimali
// -------------------------------------------------------
string parsedValue = valore.Replace(".",",");
// -------------------------------------------------------
double dValore;
double.TryParse(parsedValue, out dValore);
parsedValue = Math.Round(dValore,n).ToString().Replace(",",".");
return parsedValue;
// -------------------------------------------------------
}
Naturalmente ho postato solo la parte essenziale del codice. La funzione, infatti, è
inserita in una classe di utility.
Tengo a precisare che non è la sola soluzione possibile. Nel mio caso specifico, però,
mi va benissimo perchè devo far eseguire altri controlli all'abbandono dell'editing della
cella e quindi la struttura è necessariamente questa.