Bonjour

Il me semble decouvrir un comportement inatendu lors de l'event CellValidating d'un DataGridView

J'utilise cet event pour controler la modification d'une cellule par l'utilisateur et le cas échéant effectuer un calcul pour ajuster des cellules adjacentes

Mon datagridview est bindé sur une datatable

Ce que je constate c'est qui si durant l'event je modifie une des valeurs de la row bindée la valeur introduite par l'utilisateur ne sera pas appliquée
Un peu comme si le fait d'avoir edité la row en programme annulait l'edition par l'utilisateur ?
Est ce pertinent ?

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    // *************************************************************************
    bool AllowValidation = true;
    private void dgvModel_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
      if (!AllowValidation)
      {
        e.Cancel = true;
        return;
      }
      int icol = e.ColumnIndex;
      int irow = e.RowIndex;
 
      if (dgvModel.Rows[e.RowIndex].IsNewRow || icol < 8)
      {
        return;
      }
      if (e.FormattedValue.Equals(dgvModel[icol, irow].Value.ToString()))
      {
        return;
      }
      AllowValidation = false;
 
 
      bool Ok = Validate(icol, irow, e.FormattedValue);
      if (!Ok)
      {
        e.Cancel = true;
      }
      AllowValidation = true;
      return;
 
    }
    // *************************************************************************
    /// <summary>
    /// 8=Qty
    /// 10=Margin%
    /// 11=MarginAmt
    /// 12=Invoice
    /// <param name="icol"></param>
    /// <param name="irow"></param>
    /// <param name="p"></param>
    /// <returns></returns>
    private bool Validate(int icol, int irow, object p)
    {
 
      if (icol < 8)
      {
        return true;
      }
      int Qty = 0;
      decimal Amt = 0;
 
      DataGridViewRow dRow = dgvModel.Rows[irow];
      DataRow cRow = ((DataRowView)dRow.DataBoundItem).Row;
 
      int pkId = ToolsCvt.GetInt(cRow["pkid"]);
      CDB_OrderModel OM = curOrder.ListModel.Find(pkId);
      if (OM == null)
      {
        return false;
      }
 
      bool Ok = false;
 
      Amt = ToolsCvt.GetDecimal(p);
      C_ErrorMessage M = new C_ErrorMessage();
      switch (icol)
      {
        case 8:
          Qty = (int)Amt;
          M=OM.ChangeQt(Qty);
          if (M.isError)
          {
            MessageBox.Show(M.ErrMessage);
          }
          break;
        case 10:
          OM.ChangePrctMargin(Amt);
          break;
        case 11:
          OM.ChangeTotMargin(Amt);
          break;
        case 12:
          OM.ChangeTotInvoice(Amt);
          break;
      }
      if (!M.isError)
      {
        OM.FillRow(cRow);
      }
      return true;
    }