Pass a data from a datagridview to another datagriedview

0

what I want to do is that by clicking on the button to accept, pass the selected data from the checkbox to the second datagrid view

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(80, 800);
            this.DataGridViewSelectionArea.dataGridView.Name = "DataGridViewSelectionArea";
            this.DataGridViewSelectionArea.dataGridView.Size = new System.Drawing.Size(500, 220);


            this.Controls.Add(this.DataGridViewSectionArea.dataGridView);
            this.Controls.Add(this.DataGridViewKitchenArea.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 = true;
                        }
                    }
                }
            }
            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 = true;
                        }
                    }
                }
            }
        }

        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)
        {

        }

        private void LbKitchenArea_Click(object sender, EventArgs e)
        {

        }


    }
}
    
asked by Alejandro Estupiñán 24.12.2018 в 20:29
source

1 answer

3

To pass the selection of one grid to the other according to your code you must work at the level of DataTable or not with the control DataGridView directly but with the data you assign.

In the code I see that you define in the structure of the datatable the field

this.DataKitchenArea.Columns.Add("Sel", typeof(bool));

with which if you use DataSource of DataGridView then changing the selection of the checkbox will be impacted the change in DataTable Then you should iterate the data

var rowsSelected = this.DataKitchenArea.AsEnumerable().Where(r=> Convert.ToBoolean(r["Sel"]));

foreach(DataRow rowOrigen in rowsSelected)
{
     DataRow rowDestino = this.DataTableSection.NewRow();
     rowDestino["Sel"] = rowOrigen ["Sel"];
     rowDestino["Id"] = rowOrigen["Id"] ;
     rowDestino["Nombre"] = rowOrigen["Nombre"];
     this.DataTableSection.Rows.Add(rowDestino);
}

this.DataGridViewSectionArea.BindDataGridView(this.DataTableSection);

to not have to put a if we filter at the beginning only those selected with the help of linq, we iterate and create the new rows in the datatable of selection, finally we assign the data again to show them updated

Introduction (LINQ to DataSet)

    
answered by 26.12.2018 / 18:01
source