Depuis la version 11.0 de Mercator, ce code est obsolète. Voir : Comment permettre l'édition de colonnes supplémentaires dans une grille sans programmation ?
L'exemple de code ci-dessous montre comment ajouter une colonne modifiable dans la grille dispo. Cette colonne, dans l'exemple, permet de gérer une localisation au niveau des dépôts des différents magasins.
La programmation illustrée ici est réalisée sur base d'un customizer SigStock qui implémente les interfaces suivantes :
Le code de ce customizer s'établit comme suit :
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MercatorApi;
using MercatorUi;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Linq;
namespace SigStock
{
public class Customizer : MercatorUi.ICustomizers.IFormLoadCustomizer
, MercatorUi.ICustomizers.IFormClosedCustomizer
, MercatorUi.ICustomizers.IStringUpdater
{
public string StringUpdate(string stringToModify) // modification de la requête de la grille du disponible pour tenir compte d'une nouvelle colonne.
{
if (Api.StrExtract(stringToModify, "<ID>", "</ID>") == "STOCKDISPO") // ceci permet de ne modifier que la requête de la grille de disponible, sans modifier les requêtes des autres grilles du signalétique article
{
stringToModify = stringToModify.Replace("dispo.stockmax,", "dispo.stockmax,dispo.loc,dispo.loc as loc_old,")
.Replace("sum(stockmax),", "sum(stockmax),'','',");
}
return stringToModify;
}
public void FormLoadCustomize(Form form)
{
MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)form;
MercatorUi.Forms.Sig.SigGrids.StockDispo stockDispo = sigForm.MovableControls.Values.OfType<MercatorUi.Forms.Sig.SigGrids.StockDispo>().FirstOrDefault();
if (stockDispo != null)
{
stockDispo.AfterColumnsCreated += StockDispo_AfterColumnsCreated; // Pour ajouter la nouvelle colonne
stockDispo.AfterWrite += StockDispo_AfterWrite; // Pour écrire dans la DB les modifications
}
}
public void FormClosedCustomize(Form form)
{
MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)form;
MercatorUi.Forms.Sig.SigGrids.StockDispo stockDispo = sigForm.MovableControls.Values.OfType<MercatorUi.Forms.Sig.SigGrids.StockDispo>().FirstOrDefault();
if (stockDispo != null)
{
stockDispo.AfterColumnsCreated -= StockDispo_AfterColumnsCreated;
stockDispo.AfterWrite -= StockDispo_AfterWrite;
}
}
private void StockDispo_AfterColumnsCreated(object sender, EventArgs e) // ajouter la colonne "Localisation"
{
MercatorUi.Forms.Sig.SigGrids.StockDispo stockDispo = (MercatorUi.Forms.Sig.SigGrids.StockDispo)sender;
stockDispo.Grid.Columns.Add("loc", _Divers.Iif_langue(Globals.Langue, "Localisation", "Lokalisatie", "Localisation"));
stockDispo.Grid.Columns["loc"].Width = 85;
stockDispo.Grid.Columns["loc"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
stockDispo.Grid.Columns["loc"].DataPropertyName = "loc";
stockDispo.Grid.AddInvisibleColumn("loc_old");
}
private void StockDispo_AfterWrite(object sender, EventArgs e) // Pour écrire dans la DB les modifications sur la nouvelle colonne
{
MercatorUi.Forms.Sig.SigGrids.StockDispo stockDispo = (MercatorUi.Forms.Sig.SigGrids.StockDispo)sender;
MercatorUi.Forms.Sig.SigForm sigForm = (MercatorUi.Forms.Sig.SigForm)stockDispo.Form;
if (stockDispo.LastIdFilled == sigForm.DataSourceRow["s_id"].ToString())
{
using (SqlCommand cmd = new SqlCommand("update dispo set loc = @loc where id_stock = @id_stock and id_magasin = @id_magasin"))
{
foreach (DataGridViewRow row in stockDispo.Grid.Rows)
{
if (row.Cells["loc"].Value.ToString().TrimEnd() != row.Cells["loc_old"].Value.ToString().TrimEnd())
{
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("@id_stock", SqlDbType.Char)).Value = sigForm.DataSourceRow["s_id"].ToString();
cmd.Parameters.Add(new SqlParameter("@id_magasin", SqlDbType.Char)).Value = row.Cells["id"].Value;
cmd.Parameters.Add(new SqlParameter("@loc", SqlDbType.Char)).Value = row.Cells["loc"].Value;
Api.SqlExec(Globals.RepData, cmd);
}
}
}
}
}
}
}