Datagridview consumes unselected data

0

The problem I have is that the point of the add button is to go to the gridview on the right only the data in which the checkbox is palomated, but in this case if I do not select anything, it brings me this data: WHAT ARE THE DATAGRIDVIEWSECTIONAREA

I enclose the code of that form:

 using Parrot.Kitchen.App.Controller;
using Parrot.Kitchen.Business.Entities.Model;
using Parrot.Common.CustomComponent;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Parrot.Kitchen.App.Forms
{
    public partial class KitchenSetup : Form
    {
        private BizKitchenConfiguration bizKitchenConfiguration;
        private KitchenSetupController kitchenSetupController;
        private DataTable DataKitchenArea;
        private DataTable DataTableSection;
        private CustomDataGridView DataGridViewKitchenArea;
        private CustomDataGridView DataGridViewSectionArea;
        private CustomDataGridView DataGridViewSelectionArea;
        private MessageForm messageForm;
        private string TextMessage;
        private string KitchenAreaId;
        private string SectionAreaId;

        public KitchenSetup(BizKitchenConfiguration bizKitchenConfiguration)
        {
            this.bizKitchenConfiguration = bizKitchenConfiguration;
            this.kitchenSetupController = new KitchenSetupController(this.bizKitchenConfiguration.ConnectionIp);
            this.DataGridViewKitchenArea = new Parrot.Common.CustomComponent.CustomDataGridView();
            this.DataGridViewSectionArea = new Parrot.Common.CustomComponent.CustomDataGridView();
            this.DataGridViewSelectionArea = new Parrot.Common.CustomComponent.CustomDataGridView();
            this.TextMessage = String.Empty;
            this.AddDataGridView();
            InitializeComponent();
        }

        private void GetLicenceType()
        {
            if (this.bizKitchenConfiguration.ScreenId == -1)
            {

                this.bizKitchenConfiguration.TypeService = this.kitchenSetupController.GetLicenceType(
                    this.bizKitchenConfiguration.RestuarantId
                );
            }
            else
            {
                this.bizKitchenConfiguration.TypeService = this.kitchenSetupController.GetKitchenParameter(
                    "Service_Type",
                    this.bizKitchenConfiguration.ScreenId
                );
            }

            if (this.bizKitchenConfiguration.TypeService.Equals("Food"))
            {
                this.ChkFoodService.Checked = true;
                this.DataGridViewKitchenArea.dataGridView.Visible = true;
                this.DataGridViewSectionArea.dataGridView.Visible = true;
                this.LbSectionArea.Visible = true;
            }
            else if (this.bizKitchenConfiguration.TypeService.Equals("Quick"))
            {
                this.ChkQuickService.Checked = true;
                this.DataGridViewKitchenArea.dataGridView.Visible = true;
                this.DataGridViewSectionArea.dataGridView.Visible = false;
                this.LbSectionArea.Visible = false;

            }
        }

        private void LoadConfigDataKitchenArea()
        {
            this.DataKitchenArea = new DataTable();
            this.DataKitchenArea.Columns.Add("Sel", typeof(bool));
            this.DataKitchenArea.Columns.Add("Id", typeof(int));
            this.DataKitchenArea.Columns.Add("Nombre", typeof(string));
        }

        private void LoadConfigDataTableSection()
        {
            this.DataTableSection = new DataTable();
            this.DataTableSection.Columns.Add("Sel", typeof(bool));
            this.DataTableSection.Columns.Add("Id", typeof(int));
            this.DataTableSection.Columns.Add("Nombre", typeof(string));
        }

        private void GetScreenId()
        {
            string result = String.Empty;
            result = this.kitchenSetupController.GetScreenId();
            if (!String.IsNullOrEmpty(result))
            {
                this.bizKitchenConfiguration.ScreenId = Int32.Parse(result);
                this.TxtKitchenId.Text = result;
            }
            else
            {
                int id = this.kitchenSetupController.GetIdScreen();
                this.bizKitchenConfiguration.ScreenId = -1;
                this.TxtKitchenId.Text = (id + 1).ToString();
            }
        }

        private void GetKitchenArea()
        {
            List<BizKitchenArea> bizKitchenAreaList = this.kitchenSetupController.GetKitchenArea(
                    this.bizKitchenConfiguration.RestuarantId
                );
            if (bizKitchenAreaList != null && bizKitchenAreaList.Count >= 1)
            {
                this.LoadConfigDataKitchenArea();
                foreach (BizKitchenArea kitchenArea in bizKitchenAreaList)
                {
                    DataRow dr = this.DataKitchenArea.NewRow();
                    dr["Sel"] = false;
                    dr["Id"] = kitchenArea.Id;
                    dr["Nombre"] = kitchenArea.KitchenAreaName;
                    this.DataKitchenArea.Rows.Add(dr);
                }
                this.DataGridViewKitchenArea.BindDataGridView(this.DataKitchenArea);
                this.DataGridViewKitchenArea.AddHeaderCheckBox();

            }
        }

        private void GetTableSection()
        {
            List<BizTableSection> bizTableSection = this.kitchenSetupController.GetTableSection(
                    this.bizKitchenConfiguration.RestuarantId
                );
            if (bizTableSection != null && bizTableSection.Count >= 1)
            {
                this.LoadConfigDataTableSection();
                foreach (BizTableSection tableSection in bizTableSection)
                {
                    DataRow dr = this.DataTableSection.NewRow();
                    dr["Sel"] = false;
                    dr["Id"] = tableSection.Id;
                    dr["Nombre"] = tableSection.SectionName;
                    this.DataTableSection.Rows.Add(dr);
                }
                this.DataGridViewSectionArea.BindDataGridView(this.DataTableSection);
                this.DataGridViewSectionArea.AddHeaderCheckBox();
                this.DataGridViewSectionArea.dataGridView.Columns["Nombre"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            }
        }

        private void AddDataGridView()
        {
            // 
            // DataGridViewKitchenArea
            // 
            this.DataGridViewKitchenArea.dataGridView.Location = new System.Drawing.Point(38, 177);
            this.DataGridViewKitchenArea.dataGridView.Name = "DataGridViewKitchenArea";
            this.DataGridViewKitchenArea.dataGridView.Size = new System.Drawing.Size(250, 130);
            //
            // DataGridViewSectionArea
            // 
            this.DataGridViewSectionArea.dataGridView.Location = new System.Drawing.Point(28, 400);
            this.DataGridViewSectionArea.dataGridView.Name = "DataGridViewSectionArea";
            this.DataGridViewSectionArea.dataGridView.Size = new System.Drawing.Size(250, 108);
            //
            // DataGridViewSelectionArea
            //
            this.DataGridViewSelectionArea.dataGridView.Location = new System.Drawing.Point(550, 250);
            this.DataGridViewSelectionArea.dataGridView.Name = "DataGridViewSelectionArea";
            this.DataGridViewSelectionArea.dataGridView.Size = new System.Drawing.Size(250, 230);


            this.Controls.Add(this.DataGridViewSectionArea.dataGridView);
            this.Controls.Add(this.DataGridViewKitchenArea.dataGridView);
            this.Controls.Add(this.DataGridViewSelectionArea.dataGridView);
        }

        private void GetSelectedId()
        {
            this.KitchenAreaId = String.Empty;
            this.SectionAreaId = String.Empty;
            if (this.DataGridViewKitchenArea.dataGridView.RowCount > 0)
            {
                foreach (DataGridViewRow row in this.DataGridViewKitchenArea.dataGridView.Rows)
                {
                    if (Boolean.Parse(row.Cells["Sel"].Value.ToString()))
                    {
                        if (String.IsNullOrEmpty(this.KitchenAreaId))
                        {
                            this.KitchenAreaId += row.Cells["Id"].Value.ToString();
                        }
                        else
                        {
                            this.KitchenAreaId += "," + row.Cells["Id"].Value.ToString();
                        }
                    }
                }
            }
            if (this.DataGridViewSectionArea.dataGridView.RowCount > 0)
            {
                foreach (DataGridViewRow row in this.DataGridViewSectionArea.dataGridView.Rows)
                {
                    if (Boolean.Parse(row.Cells["Sel"].Value.ToString()))
                    {
                        if (String.IsNullOrEmpty(this.SectionAreaId))
                        {
                            this.SectionAreaId += row.Cells["Nombre"].Value.ToString();
                        }
                        else
                        {
                            this.SectionAreaId += "," + row.Cells["Nombre"].Value.ToString();
                        }
                    }
                }
            }
        }

        private void SaveKitchenSetUp()
        {
            int screenId;
            try
            {
                this.GetSelectedId();
                bool result = Int32.TryParse(TxtKitchenId.Text, out screenId);
                if (result)
                {
                    if (this.bizKitchenConfiguration.ScreenId == -1)
                    {
                        this.kitchenSetupController.SaveKitchenSetUp(
                                screenId,
                                this.KitchenAreaId,
                                this.SectionAreaId,
                                1
                            );
                        this.bizKitchenConfiguration.ScreenId = screenId;
                        this.bizKitchenConfiguration.KitchenAreaId = this.KitchenAreaId;
                        this.bizKitchenConfiguration.SectionAreaId = this.SectionAreaId;
                    }
                    else
                    {
                        this.kitchenSetupController.SaveKitchenSetUp(
                               screenId,
                               this.KitchenAreaId,
                               this.SectionAreaId,
                               2
                           );
                        this.bizKitchenConfiguration.ScreenId = screenId;
                        this.bizKitchenConfiguration.KitchenAreaId = this.KitchenAreaId;
                        this.bizKitchenConfiguration.SectionAreaId = this.SectionAreaId;
                    }
                    this.TextMessage = "La configuración de Kitchen fue guardada con éxito.";
                    this.ShowMessageForm();
                    this.Close();
                }
                else
                {
                    this.TextMessage = "Hubo un error, porfavor intente de nuevo.";
                    this.ShowMessageForm();
                }
            }
            catch
            {
                this.TextMessage = "Hubo un error, porfavor intente de nuevo.";
                this.ShowMessageForm();
            }
        }

        private void ShowMessageForm()
        {
            this.messageForm = new MessageForm();
            this.messageForm.Owner = this;
            this.messageForm.TextMessage = this.TextMessage;
            this.messageForm.ShowDialog();
            this.messageForm.Dispose();
        }

        private void SelectDataGridView()
        {
            if (!String.IsNullOrEmpty(this.bizKitchenConfiguration.KitchenAreaId))
            {
                var kitchenAreaId = this.bizKitchenConfiguration.KitchenAreaId.Split(',');

                foreach (DataGridViewRow row in this.DataGridViewKitchenArea.dataGridView.Rows)
                {
                    foreach (var id in kitchenAreaId)
                    {
                        if (row.Cells["Id"].Value.ToString().Equals(id))
                        {
                            row.Cells["Sel"].Value = false;
                        }
                    }
                }
            }
            if (!String.IsNullOrEmpty(this.bizKitchenConfiguration.SectionAreaId))
            {
                var sectionAreaId = this.bizKitchenConfiguration.SectionAreaId.Split(',');

                foreach (DataGridViewRow row in this.DataGridViewSectionArea.dataGridView.Rows)
                {
                    foreach (var id in sectionAreaId)
                    {
                        if (row.Cells["Nombre"].Value.ToString().Equals(id))
                        {
                            row.Cells["Sel"].Value = false;
                        }
                    }
                }
            }
        }

        public BizKitchenConfiguration GetKitchenSetup()
        {
            return this.bizKitchenConfiguration;
        }

        private void KitchenSetup_Load(object sender, EventArgs e)
        {

            if (this.Owner.Owner != null)
            {
                this.Owner.Enabled = false;
                this.Owner.Visible = false;
                this.Location = new Point(
                    ((this.Owner.Owner.Width - this.Width) / 2),
                    ((this.Owner.Owner.Height - this.Height) / 2)
                );
            }
            else
            {
                this.Owner.Enabled = false;
                this.Location = new Point(
                     ((this.Owner.Width - this.Width) / 2),
                     ((this.Owner.Height - this.Height) / 2)
                 );
            }
            this.TxtIpBd.Text = this.bizKitchenConfiguration.ConnectionIp;
            this.GetScreenId();
            this.GetKitchenArea();
            this.GetTableSection();
            this.GetLicenceType();
            this.SelectDataGridView();
        }

        private void KitchenSetup_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (this.Owner.Owner != null)
            {
                this.Owner.Enabled = true;
                this.Owner.Visible = true;
            }
            else
            {
                this.Owner.Enabled = true;
            }
        }

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

        private void BtnSave_Click(object sender, EventArgs e)
        {
            this.SaveKitchenSetUp();
        }

        private void btnAgregr_Click(object sender, EventArgs e)
        {
            var rowsChecked = this.DataKitchenArea.AsEnumerable().Where(r => Convert.ToBoolean(r["Sel"]));
            foreach (DataRow rowOrigen in rowsChecked)
            {
                DataRow rowDestino = this.DataTableSection.NewRow();
                rowDestino["Sel"] = false;
                rowDestino["Id"] = rowOrigen["Id"];
                rowDestino["Nombre"] = rowOrigen["Nombre"];
                this.DataTableSection.Rows.Add(rowDestino);
            }
            this.DataGridViewSelectionArea.BindDataGridView(this.DataTableSection);
        }


        private void LbKitchenArea_Click(object sender, EventArgs e)
        {

        }

        private void BtnQuitar_Click(object sender, EventArgs e)
        {
            /*   foreach(DataGridViewRow item in DataGridViewSelectionArea.SelectedRows)
               {
                   DataGridViewSelectionArea.RemoveAt(item.Index);

             }*/
            if (DataGridViewSelectionArea.SelectedRows == null)
            {
                MessageBox.Show("Debes seleccionar una fila del grid");
                return;
            }
            var dt = (DataTable)DataGridViewSelectionArea.DataSource;
            foreach (DataGridViewRow item in DataGridViewSelectionArea.SelectedRows)
            {
                var row = dt.AsEnumerable().FirstOrDefault(r => r["Id"] == item.Cells["Id"].Value);
                if (row != null)
                {
                    dt.Rows.Remove(row);
                }
                DataGridViewSelectionArea.DataSource = dt;
            }


        }
    }
}
    
asked by Alejandro Estupiñán 27.12.2018 в 18:42
source

0 answers