Monday, 20 May 2013

Checkbox in a gridview and retrive the data about choose the checkbox wise(At a time multiple checkbox choose) in Window Application

 
 ///Store Procedure At a time multiple id sent from front end
//Insert Data and at that time delete the record through the role id and insert data in to the database at that RolId
ALTER PROCEDURE [POS].[KC_InsertUserAccess]
(
@Role_ID               INT=0,
@Store_ID               INT=0,
@Parameter AS Pos.UserAccessType READONLY
)
AS
BEGIN
   SET NOCOUNT ON
    BEGIN
    BEGIN TRY
    BEGIN TRAN
   
------------------
--TRUNCATE TABLE Pos.UserAccess

DELETE FROM Pos.UserAccess
WHERE Role_ID=@Role_ID

INSERT INTO Pos.UserAccess
(Store_ID,Link_ID,Role_ID)

SELECT @Store_ID,Link_ID,@Role_ID FROM  @Parameter
------------------
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
END
SET NOCOUNT OFF
END
//
ALTER PROCEDURE POS.SelectManagerBrand
(
@Parameter VARCHAR(100)
)
AS
BEGIN

DECLARE @SQL VARCHAR(MAX)
SET @SQL=
'SELECT        IG.Item_Name, IBD.Barcode, SQA.Item_Quantity, IVD.Variation_Name, ISNULL(IP.Fixed_Price, 0) + ISNULL(IVP.Retail_Price_Inclusive, 0) AS Sale_Price,
                         MB.Brand_Name
FROM            Inventory.MST_Brand AS MB INNER JOIN
                         Inventory.D_ItemGeneralInfo AS IG INNER JOIN
                         Inventory.ItemBarcodeDetails AS IBD ON IG.Item_ID = IBD.Item_ID INNER JOIN
                         Inventory.MST_Category AS MC ON IG.Category_ID = MC.Category_ID INNER JOIN
                         Inventory.StockQCApprove AS SQA ON IBD.Barcode = SQA.Matrix_Detail_ID INNER JOIN
                         Inventory.D_ItemBrand AS IB ON IG.Item_ID = IB.Item_ID ON MB.Brand_ID = IB.Brand_ID LEFT OUTER JOIN
                         Inventory.D_ItemVariationPrice AS IVP INNER JOIN
                         Inventory.D_ItemVariationDetails AS IVD ON IVP.Variation_Detail_ID = IVD.Variation_Detail_ID ON IBD.Barcode = IVP.Barcode AND IG.Item_ID = IVD.Item_ID AND
                         IBD.Barcode = IVD.Barcode LEFT OUTER JOIN
                         Inventory.D_ItemPricing AS IP ON IG.Item_ID = IP.Item_ID AND IBD.Barcode = IP.Barcode
WHERE       MB.Brand_ID IN ('+@Parameter+')'

EXEC(@SQL)

END
--DROP TYPE Pos.SelectManagerType
--CREATE TYPE Pos.SelectManagerType
--AS TABLE
--(
--ID VARCHAR(50) NULL
--)
//


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Swash.Objects;
using Swash.BusinessLayer;
using System.Data.SqlClient;

namespace POS
{
    public partial class StoreRoomBM : Form
    {
        int rowcount = 0, nextCount = 0;
        int j, k = 0;
        int FirstIndex = 0, LastIndex = 20;
        string bids = "";
        int sum = 0;
        int BrandId = 0;
        int count = 0;
        string brandNames = "";
        List<int> lstBrand = new List<int>();
        List<string> lstNames = new List<string>();
        DataTable dtBrand;
        DataTable dtBranddtl;
        bool flag = false;
        SqlDataAdapter da = new SqlDataAdapter();
        int scrollVal;
        DataSet ds;
        DataTable dtIndex;
        int skip = 0, take = 0;


        public StoreRoomBM()
        {
            InitializeComponent();
        }

        public void CreatBrandWiseTable()
        {
            dtBranddtl = new DataTable();
            dtBranddtl.Columns.Add("ItemName", typeof(string));
            dtBranddtl.Columns.Add("Variation", typeof(string));
            dtBranddtl.Columns.Add("Barcode", typeof(string));
            dtBranddtl.Columns.Add("itemQuantity", typeof(string));
            dtBranddtl.Columns.Add("SalePrice", typeof(string));
        }

        private void StoreRoomBM_Load(object sender, EventArgs e)
        {
            txtFindBrand.Text = "Find Brand";
            AutoCompForFindBrand();
            autoItem();
            FillBrand();
            CreateDT();
            FillAllLoadTime();

            CreatBrandWiseTable();
        }
        public void GetListing()
        {

        }
      

        private void txtFindBrand_Enter(object sender, EventArgs e)
        {
            txtFindBrand.Clear();
        }
        private void txtFindBrand_Leave(object sender, EventArgs e)
        {
            try
            {
                if (txtFindBrand.Text == "")
                    txtFindBrand.Text = "Find Brand";
                else
                {
                    string s = string.Empty;
                    k = txtFindBrand.Text.LastIndexOf(" ");
                    s = txtFindBrand.Text;
                    txtFindBrand.Text = txtFindBrand.Text.Substring(0, k);
                    k = Convert.ToInt32(s.Substring(k));
                }
            }
            catch (Exception ex)
            {
            }
        }
        void AutoCompForFindBrand()
        {
            try
            {
                AutoCompleteStringCollection autocomp = new AutoCompleteStringCollection();
                var obj = ERPManagement.GetInstance.GetAllBrand();
                for (int i = 0; i < obj.Count; i++)
                {
                    autocomp.Add(obj[i].Brand_Name);
                    txtFindBrand.AutoCompleteMode = AutoCompleteMode.Suggest;
                    txtFindBrand.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    txtFindBrand.AutoCompleteCustomSource = autocomp;
                }
            }
            catch (Exception ex)
            {
            }
        }
        void autoItem()
        {
            try
            {
                AutoCompleteStringCollection autocomp = new AutoCompleteStringCollection();
                List<POS_ManagerBrandDetails> ListItem = new List<POS_ManagerBrandDetails>();
                ListItem = ERPManagement.GetInstance.AllItemDetails();
                for (int i = 0; i < ListItem.Count; i++)
                {
                    autocomp.Add(ListItem[i].ItemName);
                    TxtBrand.AutoCompleteMode = AutoCompleteMode.Suggest;
                    TxtBrand.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    TxtBrand.AutoCompleteCustomSource = autocomp;
                }
            }
            catch (Exception ex)
            {
            }
        }
        void FillBrand()
        {
            try
            {
                var AllBrand = from c in ERPManagement.GetInstance.GetAllBrand()
                               select new { c.Brand_ID, Brandname = c.Brand_Name };
                if (AllBrand.ToList().Count > 0)
                {
                    dgvAllBrand.Columns.Clear();
                    DataGridViewCheckBoxColumn Select = new DataGridViewCheckBoxColumn();
                    Select.Visible = true;
                    dgvAllBrand.Columns.Add(Select);
                    dgvAllBrand.DataSource = AllBrand.ToList();
                    dgvAllBrand.Columns["Brand_ID"].Visible = false;
                    dgvAllBrand.Columns[0].Width = 25;
                }
                else
                {
                    dgvAllBrand.DataSource = null;
                }
            }
            catch (Exception ex)
            {
            }
        }
        void SetGridHeaderNames()
        {
            DataDetailsData.Columns[0].HeaderText = "Item Name";
            DataDetailsData.Columns[1].HeaderText = "Variation Name";
            DataDetailsData.Columns[2].HeaderText = "Barcode";
            DataDetailsData.Columns[3].HeaderText = "Quantity";
            DataDetailsData.Columns[4].HeaderText = "Sale Price";
            DataDetailsData.Columns[5].HeaderText = "Brand Name";
        }
        private void dgvAllBrand_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == 0)
                {
                    DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
                    ch1 = (DataGridViewCheckBoxCell)dgvAllBrand.Rows[e.RowIndex].Cells[e.ColumnIndex];

                    if (ch1.Value == null)
                        ch1.Value = false;
                    switch (ch1.Value.ToString().ToLower())
                    {
                        case "true":
                            ch1.Value = false;
                            RemoveFromDT(Convert.ToInt32(dgvAllBrand.Rows[e.RowIndex].Cells[1].Value.ToString()));
                            lstNames.Remove(dgvAllBrand.Rows[e.RowIndex].Cells[2].Value.ToString());
                            lstBrand.Remove(Convert.ToInt32(dgvAllBrand.Rows[e.RowIndex].Cells[1].Value.ToString()));
                            UpdateBrandsString(lstBrand);
                            count--;
                            Count();
                            // UpdateBrandsString(lstNames);
                            if (bids != "")
                            {
                                var bb = (from k in ERPManagement.GetInstance.AllDataAgniastBrandID(bids).AsEnumerable()
                                          select new
                                          {
                                              Item_Name = k.Field<string>("Item_Name"),
                                              Variation_Name = k.Field<string>("Variation_Name"),
                                              Barcode = k.Field<string>("Barcode"),
                                              Item_Quantity = k.Field<int>("Item_Quantity"),
                                              Sale_Price = k.Field<decimal>("Sale_Price"),
                                              Brand_Name = k.Field<string>("Brand_Name")
                                          }).Take(15);
                                DataTable dt = new DataTable();
                                dt.Columns.Add("Item Name");
                                dt.Columns.Add("Variation Name");
                                dt.Columns.Add("Barcode");
                                dt.Columns.Add("Quantity");
                                dt.Columns.Add("Price");
                                dt.Columns.Add("BrandName");
                                foreach (var x in bb)
                                {
                                    dt.Rows.Add(x.Item_Name, x.Variation_Name, x.Barcode, x.Item_Quantity, x.Sale_Price, x.Brand_Name);
                                    dt.AcceptChanges();
                                }
                                if (bb.ToList().Count > 0)
                                {
                                    int showtotal = bb.ToList().Count;
                                    DataDetailsData.DataSource = dt;
                                    SetGridHeaderNames();
                                    var bbb = (from k in ERPManagement.GetInstance.AllDataAgniastBrandID(bids).AsEnumerable()
                                               select new
                                               {
                                                   Item_Name = k.Field<string>("Item_Name"),
                                                   Variation_Name = k.Field<string>("Variation_Name"),
                                                   Barcode = k.Field<string>("Barcode"),
                                                   Item_Quantity = k.Field<int>("Item_Quantity"),
                                                   Sale_Price = k.Field<decimal>("Sale_Price"),
                                                   Brand_Name = k.Field<string>("Brand_Name")
                                               });
                                    DataTable dt1 = new DataTable();
                                    dt1.Columns.Add("Item Name");
                                    dt1.Columns.Add("Variation Name");
                                    dt1.Columns.Add("Barcode");
                                    dt1.Columns.Add("Quantity");
                                    dt1.Columns.Add("Price");
                                    dt1.Columns.Add("BrandName");
                                    foreach (var x in bb)
                                    {
                                        dt1.Rows.Add(x.Item_Name, x.Variation_Name, x.Barcode, x.Item_Quantity, x.Sale_Price, x.Brand_Name);
                                        dt1.AcceptChanges();
                                    }
                                    LblREcordCount.Text = 1 + " - " + showtotal + "  of " + ERPManagement.GetInstance.AllDataAgniastBrandID(bids).AsEnumerable().Count();
                                    btnPrevious.Enabled = false;
                                    LblRecord.Visible = false;
                                    label2.Visible = false;
                                    LblNo.Visible = true;
                                }
                                else
                                {
                                    DataDetailsData.DataSource = dtBranddtl;
                                    label2.Visible = true;
                                    LblNo.Visible = false;
                                }
                            }
                            else
                            {
                                // DataDetailsData.DataSource = null;
                                //FillItemGridDetails(0);

                            }
                            break;
                        case "false":
                            ch1.Value = true;
                            InsertToDT(Convert.ToInt32(dgvAllBrand.Rows[e.RowIndex].Cells[1].Value.ToString()));
                            lstNames.Add(dgvAllBrand.Rows[e.RowIndex].Cells[2].Value.ToString());
                            lstBrand.Add(Convert.ToInt32(dgvAllBrand.Rows[e.RowIndex].Cells[1].Value.ToString()));
                            UpdateBrandsString(lstBrand);
                            count++;
                            Count();
                            //UpdateBrandsString(lstNames);
                            if (bids != "")
                            {
                                DataDetailsData.DataSource = null;

                                var bb = (from k in ERPManagement.GetInstance.AllDataAgniastBrandID(bids).AsEnumerable()
                                          select new
                                          {
                                              Item_Name = k.Field<string>("Item_Name"),
                                              Variation_Name = k.Field<string>("Variation_Name"),
                                              Barcode = k.Field<string>("Barcode"),
                                              Item_Quantity = k.Field<int>("Item_Quantity"),
                                              Sale_Price = k.Field<decimal>("Sale_Price"),
                                              Brand_Name = k.Field<string>("Brand_Name")
                                          }).Take(15);
                                DataTable dt = new DataTable();
                                dt.Columns.Add("Item Name");
                                dt.Columns.Add("Variation Name");
                                dt.Columns.Add("Barcode");
                                dt.Columns.Add("Quantity");
                                dt.Columns.Add("Price");
                                dt.Columns.Add("BrandName");
                                foreach (var x in bb)
                                {
                                    dt.Rows.Add(x.Item_Name, x.Variation_Name, x.Barcode, x.Item_Quantity, x.Sale_Price, x.Brand_Name);
                                    dt.AcceptChanges();
                                }
                                if (bb.ToList().Count > 0)
                                {
                                    int showtotal = bb.ToList().Count;
                                    DataDetailsData.DataSource = dt;
                                    SetGridHeaderNames();
                                    //  LblRecord.Visible = false;
                                    LblNo.Visible = true;
                                    var bbb = (from k in ERPManagement.GetInstance.AllDataAgniastBrandID(bids).AsEnumerable()
                                               select new
                                               {
                                                   Item_Name = k.Field<string>("Item_Name"),
                                                   Variation_Name = k.Field<string>("Variation_Name"),
                                                   Barcode = k.Field<string>("Barcode"),
                                                   Item_Quantity = k.Field<int>("Item_Quantity"),
                                                   Sale_Price = k.Field<decimal>("Sale_Price"),
                                                   Brand_Name = k.Field<string>("Brand_Name")
                                               });
                                    DataTable dt2 = new DataTable();
                                    dt2.Columns.Add("Item Name");
                                    dt2.Columns.Add("Variation Name");
                                    dt2.Columns.Add("Barcode");
                                    dt2.Columns.Add("Quantity");
                                    dt2.Columns.Add("Price");
                                    dt2.Columns.Add("BrandName");
                                    foreach (var x in bb)
                                    {
                                        dt2.Rows.Add(x.Item_Name, x.Variation_Name, x.Barcode, x.Item_Quantity, x.Sale_Price, x.Brand_Name);
                                        dt2.AcceptChanges();
                                    }

                                    LblREcordCount.Text = 1 + " - " + showtotal + "  of " + ERPManagement.GetInstance.AllDataAgniastBrandID(bids).AsEnumerable().Count();
                                    btnPrevious.Enabled = false;
                                    LblRecord.Visible = false;
                                }
                                else
                                {
                                    DataDetailsData.DataSource = dtBranddtl;//defaut header
                                    LblRecord.Visible = true;
                                    LblNo.Visible = false;
                                }
                            }
                            else
                            {
                                DataDetailsData.DataSource = dtBranddtl;
                                // FillItemGridDetails(0);
                            }
                            break;

                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        void Count()
        {
            if (dgvAllBrand.Rows.Count == count)
            {
                flag = true;
                chkAll.Checked = true;

                return;
            }
            else
            {
                flag = false;
                chkAll.Checked = false;
            }
        }
        private void txtFindBrand_TextChanged(object sender, EventArgs e)
        {
            //try
            //{
            //    FillBrandDetails(txtFindBrand.Text.Trim());
            //    DataDetailsData.DataSource = null;
            //    lstBrand.Clear();
            //}
            //catch (Exception ex)
            //{

            //}
        }
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                FillBrand();
                // DataDetailsData.DataSource = null;
                lstBrand.Clear();

            }
            catch (Exception ex)
            {

            }
        }
        private void BtnSave_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnFind_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtFindBrand.Text != "Find Brand")
                {
                    string name = txtFindBrand.Text.ToUpper();
                    FillDetails(name);
                    DataDetailsData.DataSource = null;
                    lstBrand.Clear();
                    //FillItemGridDetails(0);
                }
                else
                {
                    FillBrand();
                }
            }
            catch (Exception ex)
            {

            }
        }
        void FillAllLoadTime()
        {
            try
            {
                var a = from p in ERPManagement.GetInstance.AllItemDetails().Take(15)
                        select new
                        {
                            Item_Name = p.ItemName,
                            Variation_Name = p.Variation,
                            Barcode = p.Barcode,
                            Quantity = p.itemQuantity,
                            Price = p.SalePrice,
                            BrandName = p.BrandName
                        };
                DataTable dt = new DataTable();
                dt.Columns.Add("Item_Name");
                dt.Columns.Add("Variation_Name");
                dt.Columns.Add("Barcode");
                dt.Columns.Add("Quantity");
                dt.Columns.Add("Price");
                dt.Columns.Add("BrandName");
                foreach (var x in a)
                {
                    dt.Rows.Add(x.Item_Name, x.Variation_Name, x.Barcode, x.Quantity, x.Price, x.BrandName);
                    dt.AcceptChanges();
                }
                if (a.ToList().Count > 0)
                {
                    int showtotal = a.ToList().Count;
                    DataDetailsData.DataSource = dt;
                    SetGridHeaderNames();
                    LblREcordCount.Text = 1 + " - " + showtotal + "  of " + ERPManagement.GetInstance.AllItemDetails().Count;
                    btnPrevious.Enabled = false;
                    LblRecord.Visible = false;
                }
                else
                {

                    DataDetailsData.DataSource = null;
                    LblRecord.Visible = true;
                }
            }
            catch (Exception ex)
            {
            }
        }
        public bool CheckGrid()
        {
            bool retval = false;
            foreach (DataGridViewRow drg in dgvAllBrand.Rows)
            {
                DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
                ch1 = (DataGridViewCheckBoxCell)drg.Cells[0];
                if (ch1.Value != null)
                {
                    retval = true;
                    break;
                }
            }
            return retval;
        }
        private void FillDetails(string name)
        {
            try
            {
                var AllBrand = from c in ERPManagement.GetInstance.GetAllBrand()
                               where c.Brand_Name.ToUpper().StartsWith(name)
                               select new { c.Brand_ID, Brandname = c.Brand_Name };
                if (AllBrand.ToList().Count > 0)
                {
                    dgvAllBrand.Columns.Clear();
                    DataGridViewCheckBoxColumn Select = new DataGridViewCheckBoxColumn();
                    Select.Visible = true;
                    dgvAllBrand.Columns.Add(Select);
                    dgvAllBrand.DataSource = AllBrand.ToList();
                    dgvAllBrand.Columns["Brand_ID"].Visible = false;
                    dgvAllBrand.Columns[0].Width = 25;
                }
                else
                {
                    dgvAllBrand.DataSource = null;
                }
            }
            catch (Exception ex)
            {
            }
        }
        private void btnNext_Click(object sender, EventArgs e)
        {
            try
            {
                if (!CheckGrid())
                {

                    nextCount++;
                    for (int i = 1; i < ERPManagement.GetInstance.AllItemDetails().ToList().Count / 15 + 1; i++)
                    {
                        if (nextCount == i)
                        {
                            skip = skip + 15;
                            take = 15;
                            var a = from p in ERPManagement.GetInstance.AllItemDetails().Skip(skip).Take(take)
                                    select p;
                            DataTable dt = new DataTable();
                            dt.Columns.Add("Item Name");
                            dt.Columns.Add("Variation Name");
                            dt.Columns.Add("Barcode");
                            dt.Columns.Add("Quantity");
                            dt.Columns.Add("Price");
                            dt.Columns.Add("BrandName");
                            foreach (var x in a)
                            {
                                dt.Rows.Add(x.ItemName, x.Variation, x.Barcode, x.itemQuantity, x.SalePrice, x.BrandName);
                                dt.AcceptChanges();
                            }
                            if (a.ToList().Count > 0)
                            {
                                btnPrevious.Enabled = true;
                                DataDetailsData.DataSource = dt;
                                LblREcordCount.Text = (skip + 1) + " - " + (skip + a.ToList().Count) + "  of " + ERPManagement.GetInstance.AllItemDetails().Count;
                                LblRecord.Visible = false;
                            }
                            else
                            {
                                DataDetailsData.DataSource = null;
                                LblRecord.Visible = true;
                            }
                        }
                    }
                }
                else
                {
                    var bb = (from k in ERPManagement.GetInstance.AllDataAgniastBrandID(bids).AsEnumerable()
                              select new
                              {
                                  Item_Name = k.Field<string>("Item_Name"),
                                  Variation_Name = k.Field<string>("Variation_Name"),
                                  Barcode = k.Field<string>("Barcode"),
                                  Item_Quantity = k.Field<int>("Item_Quantity"),
                                  Sale_Price = k.Field<decimal>("Sale_Price"),
                                  Brand_Name = k.Field<string>("Brand_Name")
                              });
                    nextCount++;
                    for (int i = 1; i < bb.ToList().Count / 15 + 1; i++)
                    {
                        if (nextCount == i)
                        {
                            skip = skip + 15;
                            take = 15;
                            var a = (from k in ERPManagement.GetInstance.AllDataAgniastBrandID(bids).AsEnumerable()
                                     select new
                                     {
                                         Item_Name = k.Field<string>("Item_Name"),
                                         Variation_Name = k.Field<string>("Variation_Name"),
                                         Barcode = k.Field<string>("Barcode"),
                                         Item_Quantity = k.Field<int>("Item_Quantity"),
                                         Sale_Price = k.Field<decimal>("Sale_Price"),
                                         Brand_Name = k.Field<string>("Brand_Name")
                                     }).Skip(skip).Take(take);
                            DataTable dt = new DataTable();
                            dt.Columns.Add("Item Name");
                            dt.Columns.Add("Variation Name");
                            dt.Columns.Add("Barcode");
                            dt.Columns.Add("Quantity");
                            dt.Columns.Add("Price");
                            dt.Columns.Add("BrandName");
                            foreach (var x in a)
                            {
                                dt.Rows.Add(x.Item_Name, x.Variation_Name, x.Barcode, x.Item_Quantity, x.Sale_Price, x.Brand_Name);
                                dt.AcceptChanges();
                            }
                            if (a.ToList().Count > 0)
                            {
                                btnPrevious.Enabled = true;
                                DataDetailsData.DataSource = dt;
                                LblREcordCount.Text = (skip + 1) + " - " + (skip + a.ToList().Count) + "  of " + ERPManagement.GetInstance.AllDataAgniastBrandID(bids).AsEnumerable().Count();
                                if (skip + a.ToList().Count == bb.ToList().Count)
                                {
                                    btnNext.Enabled = false;
                                }
                                LblRecord.Visible = false;
                                btnPrevious.Enabled = true;
                            }
                            else
                            {

                                DataDetailsData.DataSource = null;
                                LblRecord.Visible = true;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            { }
        }
        private void btnPrevious_Click(object sender, EventArgs e)
        {
            try
            {
                if (!CheckGrid())
                {

                    nextCount--;
                    for (int i = 0; i < ERPManagement.GetInstance.AllItemDetails().ToList().Count / 15; i++)
                    {
                        if (nextCount == i)
                        {

                            skip = skip - 15;
                            take = 15;

                            var a = from p in ERPManagement.GetInstance.AllItemDetails().Skip(skip).Take(take)
                                    select p;
                            DataTable dt = new DataTable();
                            dt.Columns.Add("Item Name");
                            dt.Columns.Add("Variation Name");
                            dt.Columns.Add("Barcode");
                            dt.Columns.Add("Quantity");
                            dt.Columns.Add("Price");
                            dt.Columns.Add("BrandName");
                            foreach (var x in a)
                            {
                                dt.Rows.Add(x.ItemName, x.Variation, x.Barcode, x.itemQuantity, x.SalePrice, x.BrandName);
                                dt.AcceptChanges();
                            }
                            if (a.ToList().Count > 0)
                            {
                                //if (skip == 1)
                                //    btnPrevious.Enabled = false;
                                DataDetailsData.DataSource = dt;
                                LblREcordCount.Text = (skip + 1) + " - " + (skip + a.ToList().Count) + "  of " + ERPManagement.GetInstance.AllItemDetails().Count;
                                LblRecord.Visible = false;
                            }
                            else
                            {

                                DataDetailsData.DataSource = null;
                                LblRecord.Visible = true;
                            }
                        }
                    }
                }
                else
                {
                    var bb = (from k in ERPManagement.GetInstance.AllDataAgniastBrandID(bids).AsEnumerable()
                              select new
                              {
                                  Item_Name = k.Field<string>("Item_Name"),
                                  Variation_Name = k.Field<string>("Variation_Name"),
                                  Barcode = k.Field<string>("Barcode"),
                                  Item_Quantity = k.Field<int>("Item_Quantity"),
                                  Sale_Price = k.Field<decimal>("Sale_Price"),
                                  Brand_Name = k.Field<string>("Brand_Name")
                              });
                    nextCount--;
                    for (int i = 0; i < bb.ToList().Count / 15; i++)
                    {
                        if (nextCount == i)
                        {

                            skip = skip - 15;
                            take = 15;
                            var a = (from k in ERPManagement.GetInstance.AllDataAgniastBrandID(bids).AsEnumerable()
                                     select new
                                     {
                                         Item_Name = k.Field<string>("Item_Name"),
                                         Variation_Name = k.Field<string>("Variation_Name"),
                                         Barcode = k.Field<string>("Barcode"),
                                         Item_Quantity = k.Field<int>("Item_Quantity"),
                                         Sale_Price = k.Field<decimal>("Sale_Price"),
                                         Brand_Name = k.Field<string>("Brand_Name")
                                     }).Skip(skip).Take(take);
                            DataTable dt = new DataTable();
                            dt.Columns.Add("Item Name");
                            dt.Columns.Add("Variation Name");
                            dt.Columns.Add("Barcode");
                            dt.Columns.Add("Quantity");
                            dt.Columns.Add("Price");
                            dt.Columns.Add("BrandName");
                            foreach (var x in a)
                            {
                                dt.Rows.Add(x.Item_Name, x.Variation_Name, x.Barcode, x.Item_Quantity, x.Sale_Price, x.Brand_Name);
                                dt.AcceptChanges();
                            }
                            if (a.ToList().Count > 0)
                            {
                                //if (skip == 1)
                                //    btnPrevious.Enabled = false;
                                DataDetailsData.DataSource = dt;
                                LblREcordCount.Text = (skip + 1) + " - " + (skip + a.ToList().Count) + "  of " + ERPManagement.GetInstance.AllDataAgniastBrandID(bids).AsEnumerable().Count();
                                if (skip == 0)
                                {
                                    btnPrevious.Enabled = false;
                                }
                                btnNext.Enabled = true;
                                LblRecord.Visible = false;
                            }
                            else
                            {

                                DataDetailsData.DataSource = null;
                                LblRecord.Visible = true;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            { }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //FillItemGridDetails(0);
        }
        private void Btnok_Click(object sender, EventArgs e)
        {
            StoreRoom obj = new StoreRoom();
            //obj.StartPosition = FormStartPosition.Manual;
            ////Point p = GridViewItems.Location;
            //// obj.Location = new Point(GridViewItems.Location.X + this.Location.X, p.Y + 122); //Point(this.Location.X, 62 + this.Location.Y);
            obj.Show();
        }
        public void CreateDT()
        {
            dtBrand = new System.Data.DataTable();
            dtBrand.Columns.Add("BrandID", typeof(int));
        }
        public void InsertToDT(int bid)
        {
            try
            {
                if (dtBrand == null)
                    CreateDT();
                dtBrand.Rows.Add(bid);
                dtBrand.AcceptChanges();
            }
            catch (Exception ex)
            {
            }
        }
        public void RemoveFromDT(int bid)
        {
            try
            {
                int index = 0;
                foreach (DataRow dr in dtBrand.Rows)
                {
                    if (Convert.ToInt32(dr["BrandID"].ToString()) == bid)
                    {
                        break;
                    }
                    index++;
                }
                dtBrand.Rows.RemoveAt(index);
                dtBrand.AcceptChanges();
            }
            catch (Exception ex)
            {
            }
        }
        public void UpdateBrandsString(List<int> lst)
        {
            try
            {
                if (lst.Count > 1)
                {
                    bids = "";
                    foreach (int x in lstBrand)
                    {
                        bids = bids + x.ToString() + ",";
                    }
                    bids = bids.Remove(bids.LastIndexOf(','));
                }
                else if (lst.Count == 1)
                {
                    bids = lst[0].ToString();
                }
                else
                {
                    bids = "";
                }
            }
            catch (Exception ex)
            {
            }
        }
        private void button1_Click_1(object sender, EventArgs e)
        {
            try
            {
                if (TxtBrand.Text != "")
                {
                    string nm = TxtBrand.Text.ToUpper().Trim();
                    var nn = from bb in ERPManagement.GetInstance.AllItemDetails()
                             where bb.ItemName.ToUpper().StartsWith(nm)
                             select new { ItemName = bb.ItemName, variation = bb.Variation, barcode = bb.Barcode, quantity = bb.itemQuantity, Saleprice = bb.SalePrice, brandname = bb.BrandName };
                    DataTable dt = new DataTable();
                    dt.Columns.Add("Item Name");
                    dt.Columns.Add("Variation Name");
                    dt.Columns.Add("Barcode");
                    dt.Columns.Add("Quantity");
                    dt.Columns.Add("Price");
                    dt.Columns.Add("BrandName");
                    foreach (var x in nn)
                    {
                        dt.Rows.Add(x.ItemName, x.variation, x.barcode, x.quantity, x.Saleprice, x.brandname);
                        dt.AcceptChanges();
                    }
                    if (nn.ToList().Count > 0)
                    {
                        DataDetailsData.DataSource = dt;
                        LblRecord.Visible = false;
                    }
                    else
                    {
                        DataDetailsData.DataSource = null;
                        LblRecord.Visible = true;
                    }
                }
                else
                {
                    MessageBox.Show("Put item name.", "KenCloud", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    TxtBrand.Focus();
                }
            }
            catch (Exception ex)
            {
            }

        }
        public void UpdateBrandsString(List<string> lst)
        {
            try
            {
                if (lst.Count > 1)
                {
                    brandNames = "";
                    foreach (string x in lstNames)
                    {
                        brandNames = brandNames + x.ToString() + ",";
                    }
                    brandNames = brandNames.Remove(brandNames.LastIndexOf(','));
                }
                else if (lst.Count == 1)
                {
                    brandNames = lst[0].ToString();
                }
                else
                {
                    brandNames = "";
                }
            }
            catch (Exception ex)
            {
            }
            //TxtBrand.Text = brandNames;
        }
        private void chkAll_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (chkAll.Checked)
                {
                    if (flag == false)
                    {
                        foreach (DataGridViewRow Row in dgvAllBrand.Rows)
                        {
                            ((DataGridViewCheckBoxCell)Row.Cells[0]).Value = true;
                            InsertToDT(Convert.ToInt32(Row.Cells[1].Value.ToString()));
                            lstNames.Add(Row.Cells[2].Value.ToString());
                            lstBrand.Add(Convert.ToInt32(Row.Cells[1].Value.ToString()));
                            UpdateBrandsString(lstBrand);
                            count++;
                            Count();
                            //UpdateBrandsString(lstNames);
                            if (bids != "")
                            {
                                // DataDetailsData.DataSource = null;
                                var bb = ERPManagement.GetInstance.AllDataAgniastBrandID(bids);
                                if (bb.Rows.Count > 0)
                                {
                                    DataDetailsData.DataSource = bb;
                                    SetGridHeaderNames();
                                    LblRecord.Visible = false;
                                    LblNo.Visible = true;
                                }
                                else
                                {
                                    DataDetailsData.DataSource = dtBranddtl;//defaut header
                                    LblRecord.Visible = true;
                                    LblNo.Visible = false;
                                }
                            }
                            else
                            {
                                DataDetailsData.DataSource = dtBranddtl;
                                // FillItemGridDetails(0);
                            }
                        }
                        // FillItemGridDetails(0);
                    }
                }
                else
                {
                    if (flag == true)
                    {
                        foreach (DataGridViewRow Row in dgvAllBrand.Rows)
                        {
                            ((DataGridViewCheckBoxCell)Row.Cells[0]).Value = false;
                            count--;
                            RemoveFromDT(Convert.ToInt32(Row.Cells[1].Value.ToString()));
                            lstNames.Remove(Row.Cells[2].Value.ToString());
                            lstBrand.Remove(Convert.ToInt32(Row.Cells[1].Value.ToString()));
                            UpdateBrandsString(lstBrand);
                            Count();
                            //UpdateBrandsString(lstNames);
                            if (bids != "")
                            {
                                var bb = ERPManagement.GetInstance.AllDataAgniastBrandID(bids);
                                if (bb.Rows.Count > 0)
                                {
                                    DataDetailsData.DataSource = bb;
                                    SetGridHeaderNames();
                                    label2.Visible = false;
                                    LblNo.Visible = true;
                                }
                                else
                                {
                                    DataDetailsData.DataSource = dtBranddtl;
                                    label2.Visible = true;
                                    LblNo.Visible = false;
                                }
                            }
                            else
                            {
                                // DataDetailsData.DataSource = null;
                                //FillItemGridDetails(0);

                            }
                        }
                    }


                }
            }
            catch (Exception ex)
            {
            }

        }
        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

No comments:

Post a Comment