Ajouter une colonne read-write dans la grille de disponible de la fiche articles

0000002387     -      30/07/2025

Cette programmation est obsolète car il est possible d'obtenir le même résultat sans code via le StringUpdater de cette grille et sa propriété AutoAddCustomColumns.


La programmation décrite ici permet d'ajouter une colonne supplémentaire dans la grille de disponible de la fiche articles. Cette colonne va permettre de stocker une valeur numérique pour chaque article et pour chaque dépôt. Dans notre exemple, le champ STOCKSAI (float) a été ajouté à la table DISPO

Le code est construit sur base d'un customizer SigStock qui implémente ces interfaces :

La méthode StringUpdate va permettre de modifier la requête de lecture de la table DISPO, afin de tenir compte de la colonne ajoutée.

La méthode FormLoadCustomize va permettre d'enregistrer l'évènement AfterColumnsCreated, par lequel nous allons ajouter la colonne supplémentaire dans la grille.

La méthode SqlCommandUpdate va faire en sorte que le contenu de la colonne STOCKSAI soit sauvegardé dans la table DISPO.

Le code s'établit comme suit :

Zoom
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using MercatorUi;
using MercatorApi;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace SigStock
{
    public class Customizer : MercatorUi.ICustomizers.IFormLoadCustomizer, MercatorUi.ICustomizers.IFormClosedCustomizer, MercatorUi.ICustomizers.IStringUpdater, MercatorUi.ICustomizers.ISqlCommandUpdater
    {
        public string StringUpdate(string stringToModify)
        {
            if (Api.StrExtract(stringToModify, "<ID>", "</ID>") == "STOCKDISPO")
            {
                   stringToModify = stringToModify.Replace("dispo.qa_3,", "dispo.qa_3,dispo.stocksai,")
                                                  .Replace("sum(qa_3),", "sum(qa_3),sum(stocksai),");
            }
            return stringToModify;
        }

        public void FormLoadCustomize(Form form)
        {
            MercatorUi.Forms.Sig.SigForm SigForm = (MercatorUi.Forms.Sig.SigForm)form;
            List<Control> controls = SigForm.FindMovableControlsByType(typeof(MercatorUi.Forms.Sig.SigGrids.StockDispo));
            if (controls.Count > 0)
            {
                MercatorUi.MovableControls.MovableGrid GridDispo = (MercatorUi.MovableControls.MovableGrid)controls[0];
                GridDispo.AfterColumnsCreated += GridDispo_AfterColumnsCreated;
            }
        }

        public void FormClosedCustomize(Form form)
        {
            MercatorUi.Forms.Sig.SigForm SigForm = (MercatorUi.Forms.Sig.SigForm)form;
            List<Control> controls = SigForm.FindMovableControlsByType(typeof(MercatorUi.Forms.Sig.SigGrids.StockDispo));
            if (controls.Count > 0)
            {
                MercatorUi.MovableControls.MovableGrid GridDispo = (MercatorUi.MovableControls.MovableGrid)controls[0];
                GridDispo.AfterColumnsCreated -= GridDispo_AfterColumnsCreated;
            }
        }

        void GridDispo_AfterColumnsCreated(object sender, EventArgs e)
        {
            MercatorUi.MovableControls.MovableGrid GridDispo = (MercatorUi.MovableControls.MovableGrid)sender;

            MercatorUi.GridPro.DataGridViewDoubleTextBoxColumn c = new MercatorUi.GridPro.DataGridViewDoubleTextBoxColumn();
            c.Name = "stocksai";
            c.HeaderText = "+ Saison";
            c.Width = Api.ValSafeInt(Globals.Params["LARG_DISP2"]);
            c.DisplayFormat = Globals.PictQ;
            c.DataPropertyName = "stocksai";
            c.DefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
            c.Increment = 0;
            GridDispo.Grid.Columns.Add(c);

            GridDispo.Grid.CellValidated += grid_CellValidated;
            GridDispo.Grid.CellEnter += grid_CellEnter;
        }

        void grid_CellValidated(object sender, DataGridViewCellEventArgs e) // calculer le total de la colonne STOCKSAI
        {
            MercatorUi.GridPro.DataGridViewXPro _grid = (MercatorUi.GridPro.DataGridViewXPro)sender;

            if (((e.ColumnIndex > -1) && (_grid.Columns[e.ColumnIndex].Name == "stocksai")) && (!((_grid.Rows[e.RowIndex].Cells["id"].Value != DBNull.Value) && (_grid.Rows[e.RowIndex].Cells["id"].Value.ToString().TrimEnd() == ""))) && (_grid.RowCount > 1))
            {
                DataTable dt = (DataTable)_grid.DataSource;
                DataRow[] foundRowsTotal = dt.Select("ID = ''");
                if (foundRowsTotal.Length == 0)
                    return;
                foundRowsTotal[0][_grid.Columns[e.ColumnIndex].Name] = dt.Compute("Sum(stocksai)", "ID <> ''");
            }
        }

        void grid_CellEnter(object sender, DataGridViewCellEventArgs e) // mettre en read-only la cellule "Total" de la colonne STOCKSAI
        {
            MercatorUi.GridPro.DataGridViewXPro _grid = (MercatorUi.GridPro.DataGridViewXPro)sender;
            MercatorUi.MovableControls.MovableGrid GridDispo = (MercatorUi.MovableControls.MovableGrid)_grid.Parent;

            if ((e.ColumnIndex > -1) && (_grid.Columns[e.ColumnIndex].Name == "stocksai"))
            {
                if ((_grid.RowCount > 1) && (_grid.Rows[e.RowIndex].Cells["id"].Value != DBNull.Value) && (_grid.Rows[e.RowIndex].Cells["id"].Value.ToString().TrimEnd() == ""))
                    _grid.Columns[e.ColumnIndex].ReadOnly = true;
                else
                    _grid.Columns[e.ColumnIndex].ReadOnly = GridDispo.ReadOnly;
            }
        }

        public void SqlCommandUpdate(System.Data.SqlClient.SqlCommand SqlCommandToModify, Form WindowsForm)
        {
            string id = Api.StrExtract(SqlCommandToModify.CommandText, "<ID>", "</ID>");
            if (id == "UPDATESTOCKDISPO") // cette SqlCommand est exécutée pour toutes les lignes sauf la ligne Total
            {
                MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)WindowsForm;
                MercatorUi.MovableControls.MovableGrid GridDispo = (MercatorUi.MovableControls.MovableGrid)sigForm.FindMovableControlsByType(typeof(MercatorUi.Forms.Sig.SigGrids.StockDispo))[0];
                if (!SqlCommandToModify.CommandText.Contains("@stocksai"))
                    SqlCommandToModify.CommandText = SqlCommandToModify.CommandText.Replace(",stockmax=@stockmax", ",stockmax=@stockmax,stocksai=@stocksai");
                string id_magasin = SqlCommandToModify.Parameters["@id_magasin"].Value.ToString();
                DataRow[] foundRows = ((DataTable)GridDispo.Grid.DataSource).Select(string.Format("id='{0}'", Api.UnquoteSql(id_magasin)));
                SqlCommandToModify.Parameters.AddWithValue("@stocksai", foundRows[0]["stocksai"]);
            }
        }
    }
}

 



Cookies fonctionnels : Cookies nécessaires à l'utilisation du site et cookies de préférence. Ils ne contiennent aucune donnée à caractère personnel. (En savoir plus)

Cookies statistiques : Captation de statistiques liées aux comportements des internautes. (En savoir plus)

Cookies marketing : Pour effectuer le suivi des visiteurs au travers des sites web, à des fins publicitaires. (En savoir plus)