Club Manager : Create MemberShipPlan : Controller

Let us go back to frmMemberShipPlan and write functionality for save. In order to save we get MemberShipPlan object from form and then call AddNew or Save depending upon MemberShipPlanBeforeEdit. So we create FormToObject so that it can be used again if required.

private MemberShipPlan FormToObject()
{
    return new MemberShipPlan
    {
        MemberShip = txtMemberShip.Text,
        Description = txtDescription.Text,
        AnnualFee = txtAnnualFee.Text.ToDouble(),
        MonthlyFee = txtMonthlyFee.Text.ToDouble(),
        QuarterlyFee = txtQuarterlyFee.Text.ToDouble(),
        HalfYearlyFee = txtHalfYearlyFee.Text.ToDouble(),
        IsActive = chkIsActive.Checked
    };
}

How do we generate id of MemberShipPlan? We are writing an application which can use any database and not all of them support auto_increment field e.g. MongoDB. So we are going to implement our own ID Generation mechanism. We will create an abstract method GetID which will accept a key and get an incremented value for key from configuration table. Implementation depends on the database used and if the same supports auto_increment field we will return null and DB will handle the same.
Core.Data.Context.cs

public abstract string GetID(string key);

Core.Data.Mongo.Mongo.cs

public override string GetID(string key)
{
    return _DB.GetCollection(GetTableName(ConfigTable))
        .FindAndModify(Query.EQ("_id", key), null,
        Update.Inc("Value", 1), true, true).ModifiedDocument.GetElement("Value").Value.ToString();
}

So, now we have ID generation method as well, but where do we maintain key strings. Since key is dependent on Projects so we can not store it in any of our Core projects, but it is independent on database as well so we create a new project ClubManager.Data and store it in DBConstants class.

ClubManager.Data.DBConstants

namespace ClubManager.Data
{
    public class DBConstants
    {
        #region Config Keys
        public static string MemberShipPlanKey = "MemberShipPlan";
        #endregion Config Keys
 
        #region Table Names
        public static string MemberShipPlanTable = "MemberShipPlan";
        #endregion Table Names
    }
}

Let us now write the code to save MemberShipPlan

private void btnSave_Click(object sender, EventArgs e)
{
    MemberShipPlan temp = FormToObject();
    if (MemberShipPlanBeforeEdit == null)
    {
        temp._id = Context.DB.GetID(DBConstants.MemberShipPlanKey);
        Context.DB.AddNew(temp, DBConstants.MemberShipPlanTable);
    }
    else
    {
        temp._id = MemberShipPlanBeforeEdit._id;
        Context.DB.Save(temp, DBConstants.MemberShipPlanTable);
        MemberShipPlanBeforeEdit = null;
    }
    btnReset_Click(null, null);
}

Adding and Editing is done but what about listing them. We write listing code in new Method BindGrid(). We remove any existing columns from DataGridView, Set DataSource, Hide _id column and add Edit button in each row.

private void BindGrid()
{
    //Clear all the columns
    if (dgvMemberShipPlan.Columns.Count > 0)
        dgvMemberShipPlan.Columns.Clear();
 
    //Set Datasource
    dgvMemberShipPlan.DataSource = Context.DB.ListAll<MemberShipPlan>(DBConstants.MemberShipPlanTable);
 
    //Hide _id column
    dgvMemberShipPlan.Columns["_id"].Visible = false;
 
    //Add Edit button
    DataGridViewButtonColumn EditColumn = new DataGridViewButtonColumn();
    EditColumn.Text = "Edit";
    EditColumn.HeaderText = "Edit";
    EditColumn.UseColumnTextForButtonValue = true;
    dgvMemberShipPlan.Columns.Add(EditColumn);
}

We need to bind grid on form load, and after saving the data. So we call BindGrid() from frmMemberShipPlan() and in btnSave_Click()

How about Adding Edit Button functionality. For this we explore CellClick Event of DataGridView

private void dgvMemberShipPlan_CellClick(object sender, DataGridViewCellEventArgs e)
{
    //Get the DataGridView object
    var senderGrid = sender as DataGridView;
 
    //Check if click is in valid cell
    if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn)
    {
        switch (senderGrid.Columns[e.ColumnIndex].HeaderText)
        {
            case "Edit":
                EditPlan((MemberShipPlan)senderGrid.Rows[e.RowIndex].DataBoundItem);
                break;
        }
    }
}

So we are all done on MemberShipPlan. Let us list all the code at once.
ClubManager.frmMemberShipPlan.cs

using System;
using System.Windows.Forms;
using ClubManager.Data;
using ClubManager.Model;
using Core;
using Core.Data;
 
namespace ClubManager
{
    public partial class frmMemberShipPlan : Form
    {
        private MemberShipPlan MemberShipPlanBeforeEdit = null;
 
        public frmMemberShipPlan()
        {
            InitializeComponent();
            chkIsActive.Checked = true;
            BindGrid();
        }
 
        private void btnSave_Click(object sender, EventArgs e)
        {
            MemberShipPlan temp = FormToObject();
            if (MemberShipPlanBeforeEdit == null)
            {
                temp._id = Context.DB.GetID(DBConstants.MemberShipPlanKey);
                Context.DB.AddNew(temp, DBConstants.MemberShipPlanTable);
            }
            else
            {
                temp._id = MemberShipPlanBeforeEdit._id;
                Context.DB.Save(temp, DBConstants.MemberShipPlanTable);
                MemberShipPlanBeforeEdit = null;
            }
            btnReset_Click(null, null);
            BindGrid();
        }
 
        private void btnReset_Click(object sender, EventArgs e)
        {
            if (MemberShipPlanBeforeEdit != null)
            {
                txtMemberShip.Text = MemberShipPlanBeforeEdit.MemberShip.ToString();
                txtDescription.Text = MemberShipPlanBeforeEdit.Description.ToString();
                txtMonthlyFee.Text = MemberShipPlanBeforeEdit.MonthlyFee.ToString();
                txtQuarterlyFee.Text = MemberShipPlanBeforeEdit.QuarterlyFee.ToString();
                txtHalfYearlyFee.Text = MemberShipPlanBeforeEdit.HalfYearlyFee.ToString();
                txtAnnualFee.Text = MemberShipPlanBeforeEdit.AnnualFee.ToString();
                chkIsActive.Checked = MemberShipPlanBeforeEdit.IsActive;
            }
            else
            {
                txtMemberShip.Text =
                    txtDescription.Text =
                    txtMonthlyFee.Text =
                    txtQuarterlyFee.Text =
                    txtHalfYearlyFee.Text =
                    txtAnnualFee.Text = "";
                chkIsActive.Checked = true;
            }
        }
 
        private MemberShipPlan FormToObject()
        {
            return new MemberShipPlan
            {
                MemberShip = txtMemberShip.Text,
                Description = txtDescription.Text,
                AnnualFee = txtAnnualFee.Text.ToDouble(),
                MonthlyFee = txtMonthlyFee.Text.ToDouble(),
                QuarterlyFee = txtQuarterlyFee.Text.ToDouble(),
                HalfYearlyFee = txtHalfYearlyFee.Text.ToDouble(),
                IsActive = chkIsActive.Checked
            };
        }
 
        private void BindGrid()
        {
            //Clear all the columns
            if (dgvMemberShipPlan.Columns.Count > 0)
                dgvMemberShipPlan.Columns.Clear();
 
            //Set Datasource
            dgvMemberShipPlan.DataSource = Context.DB.ListAll<MemberShipPlan>(DBConstants.MemberShipPlanTable);
 
            //Hide _id column
            dgvMemberShipPlan.Columns["_id"].Visible = false;
 
            //Add Edit button
            DataGridViewButtonColumn EditColumn = new DataGridViewButtonColumn();
            EditColumn.Text = "Edit";
            EditColumn.HeaderText = "Edit";
            EditColumn.UseColumnTextForButtonValue = true;
            dgvMemberShipPlan.Columns.Add(EditColumn);
        }
 
        public void NewPlan()
        {
            MemberShipPlanBeforeEdit = null;
            btnReset_Click(null, null);
            this.Show();
            this.Focus();
        }
 
        public void EditPlan(MemberShipPlan MemberShipPlan)
        {
            MemberShipPlanBeforeEdit = MemberShipPlan;
            btnReset_Click(null, null);
            this.Show();
            this.Focus();
        }
 
        private void dgvMemberShipPlan_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //Get the DataGridView object
            var senderGrid = sender as DataGridView;
 
            //Check if click is in valid cell
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn)
            {
                switch (senderGrid.Columns[e.ColumnIndex].HeaderText)
                {
                    case "Edit":
                        EditPlan((MemberShipPlan)senderGrid.Rows[e.RowIndex].DataBoundItem);
                        break;
                }
            }
        }
 
        private void frmMemberShipPlan_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }
    }
}

Leave a Reply