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) // modification de la requête de lecture des données
{
if (StringToModify.StartsWith("select id,nom from reps order by nom")) // on est dans l'écran de paramétrage des représentants
return StringToModify.Replace("select id,nom", "select id,nom,email");
else
return StringToModify; // on est dans un autre écran de paramétrage -> on ne change rien
}
public void FormLoadCustomize(Form WindowsForm)
{
if (WindowsForm is MercatorUi.Forms.Param.ParamRepsForm) // on est dans l'écran de paramétrage des représentants
{
MercatorUi.Forms.Param.ParamRepsForm paramRepsForm = (MercatorUi.Forms.Param.ParamRepsForm)WindowsForm;
// ajout de la colonne email
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);
// ajout de la colonne bouton Outlook
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);
// installation de 2 events
paramRepsForm.Grid.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(Grid_CellFormatting);
paramRepsForm.Grid.CellSuperClick += new MercatorUi.GridPro.CellSuperClickHandler(Grid_CellSuperClick);
// réduire la largeur de la colonne "Nom" qui est de toute façon trop large
paramRepsForm.Grid.Columns[0].Width = 160;
}
}
public void FormClosedCustomize(Form WindowsForm)
{
if (WindowsForm is MercatorUi.Forms.Param.ParamRepsForm) // on est dans l'écran de paramétrage des représentants
{
MercatorUi.Forms.Param.ParamRepsForm paramRepsForm = (MercatorUi.Forms.Param.ParamRepsForm)WindowsForm;
// désinscrire les 2 events
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") // la cellule en cours de formattage est bien le bouton. On peut donc disabler le bouton quand il n'y a pas d'adresse mail
{
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") // la cellule sur laquelle on a cliqué est bien le bouton Outlook
{
MercatorUi.GridPro.DataGridViewHidableButtonXCell cellBtn = (MercatorUi.GridPro.DataGridViewHidableButtonXCell)Grid.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (!cellBtn.Disable) // si le bouton n'est pas disablé, on peut lancer Outlook
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) // modification de la requête de sauvegarde des données
{
if (WindowsForm is MercatorUi.Forms.Param.ParamRepsForm) // on est bien dans l'écran de paramétrage des représentants
{
if (!SqlCommandToModify.CommandText.Contains("email")) // cette modification ne doit être effectuée qu'une seule fois. Ensuite le requête modifiée est utilisée pour toutes les lignes de la grille qui ont été ajoutées/modifiées
{
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(); // il nous faut connaître l'ID de la ligne afin de retrouver l'adresse mail correspondante à cette ligne.
DataTable dt = (DataTable)paramRepsForm.Grid.DataSource; // derrière la grille, se trouve en fait une DataTable
DataRow[] foundRows = dt.Select(string.Format("id='{0}'", Api.UnquoteSql(id))); // recherche de l'adresse mail sur base de l'ID. UnquoteSql permet de doubler les quotes si l'id en contient
if (foundRows.Length > 0) // en principe, cette condition sera toujours remplie
SqlCommandToModify.Parameters.Add("@email", SqlDbType.Char).Value = foundRows[0]["email"].ToString(); // on ajoute le paramètre SQL @email avec la bonne valeur
}
}
}
}