using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MercatorApi;
using MercatorUi;
using System.Windows.Forms;
// <CompileWithRoslyn />
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 query 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 form)
{
if (form is MercatorUi.Forms.Param.ParamRepsForm paramRepsForm) // We zijn in het parameterinstellingscherm van de vertegenwoordigers
{
// 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 += Grid_CellFormatting;
paramRepsForm.Grid.CellSuperClick += 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 form)
{
if (form is MercatorUi.Forms.Param.ParamRepsForm paramRepsForm) // We zijn in het parameterinstellingscherm van de vertegenwoordigers
{
// De 2 events uitschrijven
paramRepsForm.Grid.CellFormatting -= Grid_CellFormatting;
paramRepsForm.Grid.CellSuperClick -= 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 sqlCommandToUpdate, Form form) // Wijziging van de opdracht voor gegevensopslag
{
if (form is MercatorUi.Forms.Param.ParamRepsForm paramRepsForm) // We zijn wel degelijk in het parameterinstellingscherm van de vertegenwoordigers
{
if (!sqlCommandToUpdate.CommandText.Contains("email")) // Deze wijziging moet slechts eenmaal worden uitgevoerd. Daarna wordt de gewijzigde opdracht gebruikt voor alle toegevoegde/gewijzigde rijen van het rooster
{
sqlCommandToUpdate.CommandText = sqlCommandToUpdate.CommandText.Replace("nom=@nom", "nom=@nom,email=@email");
sqlCommandToUpdate.CommandText = sqlCommandToUpdate.CommandText.Replace("(id,nom)", "(id,nom,email)");
sqlCommandToUpdate.CommandText = sqlCommandToUpdate.CommandText.Replace("(@id,@nom)", "(@id,@nom,@email)");
}
string id = sqlCommandToUpdate.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
sqlCommandToUpdate.Parameters.Add("@email", SqlDbType.Char).Value = foundRows[0]["email"].ToString(); // Men voegt de SQL-parameter @email in met de juiste waarde
}
}
}
}