In de lijst van de vertegenwoordigers een kolom met het e-mailadres toevoegen en een knop voor het sturen van een mail

0000002007     -      29-10-2013

De hieronder beschreven module illustreert een Param-customizer. Dit type customizer laat toe om de parameterinstellingschermen van Mercator te bewerken. Deze programmering is bedoeld om in de lijst van de vertegenwoordigers een kolom met het e-mailadres toe te voegen, alsook een knop voor het sturen van een mail via Outlook. In ons voorbeeld hebben we het veld email char(35) toegevoegd in de tabel REPS.

param_reps_email_N

De code implementeert de volgende interfaces:

attention We vestigen de aandacht op het feit dat de code van een Param-customizer in alle parameterschermen wordt uitgevoerd. Het is dus essentieel dat men bij alle gebruikte methodes nagaat of men wel degelijk in het gewenste scherm staat, in ons voorbeeld is dat het scherm van de vertegenwoordigers .

De code ziet er als volgt uit:

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

namespace Param
{
     public class Customizer : MercatorUi.ICustomizers. IStringUpdater ,
                              MercatorUi.ICustomizers. IFormLoadCustomizer ,
                              MercatorUi.ICustomizers. IFormClosedCustomizer ,
                              MercatorUi.ICustomizers. ISqlCommandUpdater
    {

         public string StringUpdate( string StringToModify) // Wijziging van de opdracht voor het lezen van de gegevens
        {
             if (StringToModify.StartsWith( "select id,nom from reps order by nom" )) // We zijn in het parameterinstellingscherm van de vertegenwoordigers
                 return StringToModify.Replace( "select id,nom" , "select id,nom,email" );
             else
                 return StringToModify; // we zijn in een ander parameterinstellingscherm -> we veranderen niets
        }

         public void FormLoadCustomize( Form WindowsForm)
        {
             if (WindowsForm is MercatorUi.Forms.Param. ParamRepsForm ) // We zijn in het parameterinstellingscherm van de vertegenwoordigers
            {
                MercatorUi.Forms.Param. ParamRepsForm paramRepsForm = (MercatorUi.Forms.Param. ParamRepsForm )WindowsForm;

                 // Toevoeging van de kolom e-mail
                 DataGridViewTextBoxColumn colEmail = new DataGridViewTextBoxColumn ();
                colEmail.Name = "email" ;
                colEmail.HeaderText = "Email" ;
                colEmail.Width = 160;
                colEmail.DataPropertyName = "email" ;
                colEmail.MaxInputLength = 35;
                colEmail.SortMode = DataGridViewColumnSortMode .NotSortable;
                paramRepsForm.Grid.Columns.Add(colEmail);

                 // Toevoeging van de kolom voor de Outlook-knop
                MercatorUi.GridPro. DataGridViewHidableButtonXColumn btnCol = new MercatorUi.GridPro. DataGridViewHidableButtonXColumn ();
                btnCol.Name = "btnCol" ;
                btnCol.Image = _Divers .ImageFromResource( "mail" );
                btnCol.Width = 34;
                btnCol.HeaderText = "" ;
                paramRepsForm.Grid.Columns.Add(btnCol);

                 // Installatie van 2 events
                paramRepsForm.Grid.CellFormatting += new System.Windows.Forms. DataGridViewCellFormattingEventHandler (Grid_CellFormatting);
                paramRepsForm.Grid.CellSuperClick += new MercatorUi.GridPro. CellSuperClickHandler (Grid_CellSuperClick);

                 // De breedte verminderen van de kolom "Naam" die in elk geval te breed is
                paramRepsForm.Grid.Columns[0].Width = 160;
            }
        }

         public void FormClosedCustomize( Form WindowsForm)
        {
             if (WindowsForm is MercatorUi.Forms.Param. ParamRepsForm ) // We zijn in het parameterinstellingscherm van de vertegenwoordigers
            {
                MercatorUi.Forms.Param. ParamRepsForm paramRepsForm = (MercatorUi.Forms.Param. ParamRepsForm )WindowsForm;

                 // De 2 events uitschrijven
                paramRepsForm.Grid.CellFormatting -= new System.Windows.Forms. DataGridViewCellFormattingEventHandler (Grid_CellFormatting);
                paramRepsForm.Grid.CellSuperClick -= new MercatorUi.GridPro. CellSuperClickHandler (Grid_CellSuperClick);
            }
        }

         private void Grid_CellFormatting( object sender, DataGridViewCellFormattingEventArgs e)
        {
            MercatorUi.GridPro. DataGridViewXPro Grid = (MercatorUi.GridPro. DataGridViewXPro )sender;
             if ((e.RowIndex > -1) && (e.ColumnIndex > -1))
            {
                 if (Grid.Columns[e.ColumnIndex].Name == "btnCol" ) // De cel die momenteel wordt geformatteerd, is duidelijk de knop. We kunnen de knop dus deactiveren wanneer er geen e-mailadres is
                {

                    MercatorUi.GridPro. DataGridViewHidableButtonXCell cellBtn = (MercatorUi.GridPro. DataGridViewHidableButtonXCell )Grid.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    cellBtn.Disable = (Grid.Rows[e.RowIndex].Cells[ "email" ].Value == DBNull .Value) || (Grid.Rows[e.RowIndex].Cells[ "email" ].Value == null ) || (Grid.Rows[e.RowIndex].Cells[ "email" ].Value.ToString().Trim() == "" );
                }

            }
        }

         private void Grid_CellSuperClick( object sender, DataGridViewCellEventArgs e)
        {
            MercatorUi.GridPro. DataGridViewXPro Grid = (MercatorUi.GridPro. DataGridViewXPro )sender;
             if ((e.RowIndex > -1) && (e.ColumnIndex > -1))
            {
                 if (Grid.Columns[e.ColumnIndex].Name == "btnCol" ) // De cel waarop we hebben geklikt, is inderdaad de Outlook-knop
                {
                    MercatorUi.GridPro. DataGridViewHidableButtonXCell cellBtn = (MercatorUi.GridPro. DataGridViewHidableButtonXCell )Grid.Rows[e.RowIndex].Cells[e.ColumnIndex];
                     if (!cellBtn.Disable) // Als de knop niet is gedeactiveerd, kunnen we Outlook opstarten
                        MercatorOutlook. OutlookStatic .SendMail( "" , Grid.Rows[e.RowIndex].Cells[ "nom" ].Value.ToString().Trim(), Grid.Rows[e.RowIndex].Cells[ "email" ].Value.ToString().Trim(), "" , null );

                }
            }
        }

         public void SqlCommandUpdate(System.Data.SqlClient. SqlCommand SqlCommandToModify, Form WindowsForm) // Wijziging van de opdracht voor gegevensopslag
        {
             if (WindowsForm is MercatorUi.Forms.Param. ParamRepsForm ) // We zijn wel degelijk in het parameterinstellingscherm van de vertegenwoordigers
            {
                 if (!SqlCommandToModify.CommandText.Contains( "email" )) // Deze wijziging moet slechts eenmaal worden uitgevoerd. Daarna wordt de gewijzigde opdracht gebruikt voor alle toegevoegde/gewijzigde rijen van het rooster
                {
                    SqlCommandToModify.CommandText = SqlCommandToModify.CommandText.Replace( "nom=@nom" , "nom=@nom,email=@email" );
                    SqlCommandToModify.CommandText = SqlCommandToModify.CommandText.Replace( "(id,nom)" , "(id,nom,email)" );
                    SqlCommandToModify.CommandText = SqlCommandToModify.CommandText.Replace( "(@id,@nom)" , "(@id,@nom,@email)" );
                }
                MercatorUi.Forms.Param. ParamRepsForm paramRepsForm = (MercatorUi.Forms.Param. ParamRepsForm )WindowsForm;
                 string id = SqlCommandToModify.Parameters[ "@id" ].Value.ToString(); // We moeten de ID van de rij kennen om het e-mailadres te vinden dat bij deze rij hoort.
                 DataTable dt = ( DataTable )paramRepsForm.Grid.DataSource; // Achter het rooster bevindt er zich in feite een DataTable
                 DataRow [] foundRows = dt.Select( string .Format( "id='{0}'" , Api .UnquoteSql(id))); // Het e-mailadres zoeken op basis van de ID. Via UnquoteSql kunnen eventuele quotes in de ID worden gekopieerd.
                 if (foundRows.Length > 0) // In principe zal steeds voldaan zijn aan deze voorwaarde.
                    SqlCommandToModify.Parameters.Add( "@email" , SqlDbType .Char).Value = foundRows[0][ "email" ].ToString(); // Men voegt de SQL-parameter @email in met de juiste waarde.
            }
        }

    }
}

De door StringUpdate gewijzigde standaard-SQL-opdracht ziet er als volgt uit:

    select id,nom from reps order by nom
    select dbo.FIELD_LENGTH('reps','nom')

Het door SqlCommandUpdate gewijzigde SQL-opslagscript ziet er als volgt uit:

    if exists(select * from reps where id=@id)
        update reps set nom=@nom where id=@id
    else
        insert into reps (id,nom) values (@id,@nom)