none
how to save data from Form2 datagridview data to Form1 datagridview using c#

    Question

  • Hi to all,

    i created form1 which contains product details & form2 contains the product series details.on select product combo box in datagridview from1 it'll show the new button & after clicking the button i'll open new form form2 in that i'll add details of product series to datagridview in form2 Then i'll save the data next this should saved in the form1 datagridview  with corresponding row where button is clicked add series details, & while saving data to database, I'll save product details along with series details.

    my query is "Is it possible save data from form2 to from1 datagridview or using like session control in web application?"

    tags:sql server 2008,visual studio 2010,c#

    my form1 code

    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 BusinessLayer;
    using FacadeLayer;

    namespace DynamicGridProject
    {
        public partial class ProductGridForm : Form
        {
            CProductBL objproduct = new CProductBL();
            CProductFL objprodctFL = new CProductFL();
            int intRowIndex = 0,intRowCmValue=0;
          
            ComboBox combo;

            private DataTable _details;

            public DataTable ps_details
            {
                get { return _details; }
                set { _details = value; }
            }
            public ProductGridForm()
            {
                InitializeComponent();
            }

            private void ProductGridForm_Load(object sender, EventArgs e)
            {
                DataSet ds = new DataSet();
                ds = objprodctFL.GetProductsById(objproduct);
                DataGridViewComboBoxColumn cmbcolumn = new DataGridViewComboBoxColumn();
                cmbcolumn.HeaderText = "Product Name ";
                cmbcolumn.DataSource = ds.Tables[0];
                cmbcolumn.DisplayMember = "product_name";
                cmbcolumn.ValueMember = "product_id";
                dgvProductGrid.Columns.Insert(1, cmbcolumn);
                DataGridViewButtonColumn btnclmn = new DataGridViewButtonColumn();
                btnclmn.Name = "btn";
                btnclmn.HeaderText = "btn";
                dgvProductGrid.Columns.Insert(2, btnclmn);
                dgvProductGrid.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
            }

            private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                combo = e.Control as ComboBox;
                if (combo != null)
                {
                    combo.BackColor = Color.White;
                    combo.SelectedValue = intRowCmValue;
                    combo.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
                    combo.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
                }
            }

            private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
            {
                ComboBox cb = (ComboBox)sender;
                intRowCmValue = 0;
                if (cb != null)
                {
                    ((ComboBox)sender).BackColor = Color.White;
                    //  string strId = dgvProductGrid.CurrentCell.Value.ToString();
                    DataGridViewComboBoxCell objcell = new DataGridViewComboBoxCell();
                    objcell = (DataGridViewComboBoxCell)dgvProductGrid.Rows[dgvProductGrid.CurrentRow.Index].Cells[1];
                    if (objcell.Value != null)
                    {
                        intRowCmValue = int.Parse(objcell.Value.ToString());
                    }
                    DataRowView view = cb.SelectedItem as DataRowView;
                    if (view != null)
                    {
                        dgvProductGrid.Rows[dgvProductGrid.CurrentRow.Index].Cells["manufacturer"].Value = view.Row.ItemArray[2].ToString();
                        dgvProductGrid.Rows[dgvProductGrid.CurrentRow.Index].Cells["model"].Value = view.Row.ItemArray[3].ToString();
                        dgvProductGrid.Rows[dgvProductGrid.CurrentRow.Index].Cells["product_description"].Value = view.Row.ItemArray[4].ToString();
                        dgvProductGrid.Rows[dgvProductGrid.CurrentRow.Index].Cells["amc_priority"].Value = view.Row.ItemArray[5].ToString();
                        dgvProductGrid.Rows[dgvProductGrid.CurrentRow.Index].Cells["amc_duration"].Value = view.Row.ItemArray[6].ToString();
                        dgvProductGrid.Rows[dgvProductGrid.CurrentRow.Index].Cells["amc_cost"].Value = view.Row.ItemArray[7].ToString();
                    }
                    else
                    {
                        combo.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
                    }
                }
            }

            private void color_change(object sender, EventArgs e)
            {
                this.BackColor = Color.White;
                this.ForeColor = Color.Black;
            }

            private void dgvProductGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == dgvProductGrid.Columns["btn"].Index)
                {
                    intRowIndex = dgvProductGrid.CurrentRow.Index;
                    DataGridViewComboBoxCell objcell = new DataGridViewComboBoxCell();
                    objcell = (DataGridViewComboBoxCell)dgvProductGrid.Rows[intRowIndex].Cells[1];
                    if (objcell != null && objcell.Value!=null)
                    {
                        string strId = objcell.Value.ToString();
                        string strName = objcell.EditedFormattedValue.ToString();
                        string strnameval = objcell.FormattedValue.ToString();
                        ProdSeriesForm objpsform = new ProdSeriesForm();
                        objpsform.ps_Product_id = int.Parse(strId);
                        objpsform.ps_product_name = strName;
                        objpsform.ps_row_number = intRowIndex;
                        objpsform.ShowDialog();
                    }
                }
            }

            private void btnprdSave_Click(object sender, EventArgs e)
            {

            }

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

    form2 code

    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 BusinessLayer;

    namespace DynamicGridProject
    {
        public partial class ProdSeriesForm : Form
        {
            private int _product_id, _row_number;
            private string _product_name;

            public int ps_Product_id
            {
                get { return _product_id; }
                set { _product_id = value; }
            }
            public string ps_product_name
            {
                get { return _product_name; }
                set { _product_name = value; }
            }
            public int ps_row_number
            {
                get { return _row_number; }
                set { _row_number = value; }
            }

            private DataTable _details;

            public DataTable ps_details
            {
                get { return _details; }
                set { _details = value; }
            }

            public ProdSeriesForm()
            {
                InitializeComponent();
            }

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

            private void btnPSSave_Click(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("rownumber");
                dt.Columns.Add("intProdSeriesId");
                dt.Columns.Add("strProdSeriesNumber");
                dt.Columns.Add("product_id");
                dt.Columns.Add("dtStartDate");
                dt.Columns.Add("dtEndDate");
                dt.Columns.Add("strRemark");
              
                DataRow dr = dt.NewRow();
                dr["rownumber"] = _row_number;
                dr["intProdSeriesId"] = 0;
                dr["strProdSeriesNumber"] = txtpsname.Text.Trim();
                dr["product_id"] = ps_Product_id;
                dr["dtStartDate"] = dateTimePicker1.Text;
                dr["dtEndDate"] = dateTimePicker2.Text;
                dr["strRemark"] = "";
                dt.Rows.Add(dr);
                ProductGridForm objproduct = new ProductGridForm();
                objproduct.ps_details = dt;
                this.Close();
            }

            private void ProdSeriesForm_Load(object sender, EventArgs e)
            {
                lblheader.Text = "Product Name :  " + ps_product_name;
            }
        }
    }

    Friday, July 27, 2012 5:07 AM

Answers

All replies