Accounting Project Part 7 Accounts Head Form in C#

Hi, Dear's here we learn how to implement stock in Accounting Project  POS in Visual Studio using C#. Ilyasoft software company provide full project step by step training on our YouTube Channel ilyasoft software company so now subscribe, share and like.

In this tutorial we will cover all the below mention topics:
    i)      How to Create AccountForms Folder's inside Forms Folder in visual studio?
    ii)     How to Create Account Head Forms inside AccountForms  folder in our project?
    iii)    How to design Account Head form?
    iv)    How to Insert, update, delete and retrieve records from AccountHeadTable and show inside datagridview(dgv)?

So if you want all question answers watch the video, and for source code scroll down.


Design Account Head Form in C# / C-Sharp :
So in this example, we have account head form, show below in figure:

Source Code Account Head Form : 
using System;
using System;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace POSAccounts.Forms.AccountForms
{
    public partial class FrmAccountHead : Form
    {
        public FrmAccountHead()
        {
            InitializeComponent();
            PanelBar.MouseDown += PanelBar_MouseDown;
        }

        private const int HT_CAPTION = 0x2; // what does this means
        private const int WM_NCLBUTTONDOWN = 0x00A1; // what does this means
        [DllImport("user32", CharSet = CharSet.Auto)]// what does this means
        private static extern bool ReleaseCapture();
        [DllImport("user32", CharSet = CharSet.Auto)]// what does this means
        private static extern int SendMessage(
        IntPtr hwnd,
        int wMsg,
        int wParam,
        int lParam);
        private void PanelBar_MouseDown(object sender, MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
            {
                Rectangle rct = DisplayRectangle;
                if (rct.Contains(e.Location))
                {
                    ReleaseCapture();
                    SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
                }
            }
        }

        private void FrmAccountHead_Load(object sender, EventArgs e)
        {
            FillGrid(string.Empty);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close(); // using to close current form
        }

        private void btnMinimize_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.WindowState = FormWindowState.Maximized;
            }
            else
            {
                this.WindowState = FormWindowState.Minimized;
            }
        }

        // Retrive Data From Account Head Table using Select Method of Database layer
        private void FillGrid(string searchvalue)
        {
            try
            {
                string query = "Select HeadID [ID], HeadTitle [Account Head] FROM AccountHeadTable";
                if (!string.IsNullOrEmpty(searchvalue))
                {
                    query = "Select HeadID [ID], HeadTitle [Account Head] FROM AccountHeadTable Where HeadTitle like '%" + searchvalue.Trim() + "%'";
                }

                DataTable dt = DatabaseLayer.Select(query);
                dgv.DataSource = dt;
                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                        //dgv.Columns[0].Width = 80; // Head ID
                        dgv.Columns[0].Visible = false; // Head ID
                        dgv.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; // Account Head Title
                    }
                }
            }
            catch
            {
                dgv.DataSource = null;
            }
        }

        // Validation Method for More Details Watch Vedio
        private void EnableComponents()
        {
            btnCancel.Visible = true;
            btnUpdate.Visible = true;
            btnSave.Visible = false;
            btnClear.Visible = false;
            dgv.Enabled = false;
            txtSearch.Enabled = false;
        }

        // Valition Method for More Details Watch Vedio
        private void ResetComponents()
        {
            btnCancel.Visible = false;
            btnUpdate.Visible = false;
            btnSave.Visible = true;
            btnClear.Visible = true;
            dgv.Enabled = true;
            txtSearch.Enabled = true;
            txtAccountHead.Clear();
            FillGrid(string.Empty);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            // Clear Account Head Title Name
            txtAccountHead.Clear();
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            FillGrid(txtSearch.Text.Trim());
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ResetComponents();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                ep.Clear();
                // First Check Account Head Textbox, if it's is empty then show message to user, plese enter Account Head Title
                if (txtAccountHead.Text.Trim().Length == 0)
                {
                    ep.SetError(txtAccountHead, "Please Enter Account Head Title!");
                    txtAccountHead.Focus();
                    return;
                }

                // Check Account Head is already registered or not
                DataTable existdt = DatabaseLayer.Select("Select * From AccountHeadTable Where  HeadTitle = '" + txtAccountHead.Text.Trim() + "'");
                if (existdt != null)
                {
                    if (existdt.Rows.Count > 0)
                    {
                        ep.SetError(txtAccountHead, "Already Exist!"); // Registered show this message
                        txtAccountHead.Focus();
                        txtAccountHead.SelectAll();
                        return;
                    }
                }

                // Now use database layer insert method to insert data in database in AccountHeadTable.
                string insertquery = string.Format("Insert into AccountHeadTable (HeadTitle) values('{0}')", txtAccountHead.Text.Trim());
                bool result = DatabaseLayer.Insert(insertquery); // user insert method for insert Account Head in AccountHeadTable
                if (result == true)
                {
                    MessageBox.Show("Registered Successfully!"); // if Insert show success message
                    FillGrid(string.Empty);
                    txtAccountHead.Clear();
                }
                else
                {
                    MessageBox.Show("Please Provide Correct Details!"); // if Not Insert show success message
                }
            }
            catch
            {
                // incase of any issue is occure show this message
                MessageBox.Show("Some Unexptected issue is occure plz try agian!");
            }
        }

        // it's context menu strip edit tool click event, using to select record to edit 
        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgv != null)
            {
                if (dgv.Rows.Count > 0)
                {
                    if (dgv.SelectedRows.Count == 1)
                    {
                        if (MessageBox.Show("Are you sure you want to update selected Account Head?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            txtAccountHead.Text = dgv.Rows[dgv.SelectedRows[0].Index].Cells[1].Value.ToString();
                            EnableComponents();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please Select One Record!");
                    }
                }
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                ep.Clear();
                // First Check Account Head Textbox, if it's is empty then show message to user, plese enter Account Head name
                if (txtAccountHead.Text.Trim().Length == 0)
                {
                    ep.SetError(txtAccountHead, "Please Enter Account Head Title!");
                    txtAccountHead.Focus();
                    return;
                }

                // Check Account Head Title is already exist in table or not
                DataTable existdt = DatabaseLayer.Select("Select * From AccountHeadTable Where  HeadTitle = '" + txtAccountHead.Text.Trim() + "' AND HeadID != '" + dgv.Rows[dgv.SelectedRows[0].Index].Cells[0].Value + "'");
                if (existdt != null)
                {
                    if (existdt.Rows.Count > 0)
                    {
                        ep.SetError(txtAccountHead, "Already Exist!"); // Show Existance Message near txtAccountHead Textbox
                        txtAccountHead.Focus();
                        txtAccountHead.SelectAll();
                        return;
                    }
                }

                // Now use database layer update method to update database in AccountHeadTable.
                string updatequery = string.Format("Update AccountHeadTable  set HeadTitle = '{0}' where HeadID = '{1}'", txtAccountHead.Text.Trim(), dgv.Rows[dgv.SelectedRows[0].Index].Cells[0].Value);
                bool result = DatabaseLayer.Update(updatequery);
                if (result == true)
                {
                    MessageBox.Show("Updated Successfully!"); // if update show success message
                    txtAccountHead.Clear();
                    ResetComponents();
                }
                else
                {
                    MessageBox.Show("Please Check in Try Again!"); // if not updated, show please proivde correct details
                }
            }
            catch
            {
                // incase of any issue is occure show this message
                MessageBox.Show("Some Unexptected issue is occure plz try agian!");
            }
        }

        private void txtAccountHead_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

Comments