Click here to Skip to main content
15,902,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have to forms ... the first one is form_Items_Transaction with datagridview (Dgv_transactions) and the second one is Form_Search_Items with datagridview(Dgv_Search)... i have fill dgv_transactions with datatable.so i want to fill column 7 from dgv_transaction with column 1 in dgv_Search with the event Dgv_Transaction_KeyDown.but the problem is that when i close Form_Search_Items there is no change in column[7] in Dgv_Search.

can any one help plz?

What I have tried:

C#
private void Dgv_Transaction_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (Dgv_Transaction.CurrentRow.Cells[7].Selected == true)
                {
                    if (e.KeyCode == Keys.Escape)
                    {
                        string col1;
                        string col2;
                       
                        
                        DataTable dtcust = Class_Main_Units.StoredProcedure_Search_Items();

                        Form_Search cust = new Form_Search_Units();
                        cust.Dgv_Search.DataSource = dtcust;
                        
                        cust.ShowDialog();

                        col1 = cust.Dgv_Search.CurrentRow.Cells[0].Value.ToString();
                        col2 = cust.Dgv_Search.CurrentRow.Cells[1].Value.ToString();
                        

                        Dgv_Transaction.CurrentRow.Cells[7].Value = col2;
                        Dgv_Transaction.CurrentRow.Cells[6].Value = col1;

                    }
                }
            }
            catch
            {

            }
            
        }
Posted
Updated 21-Dec-18 20:49pm
v2

1 solution

You could put the DataTable and BindingSource in a Static class, then you can access them from all forms.

Program.cs
using System;
using System.Windows.Forms;
using System.ComponentModel;

namespace TestForm1
{
    static class Program
    {
        // for Form10 DataGridView.
        public static BindingList<MyClass> masterBindingList;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // Test data grid with BindingList.
            Application.Run(new Form10());
        }

        /// <summary>
        /// For Form10:
        /// Simple class with one string.
        /// </summary>
        public class MyClass
        {
            public string Title { get; set; }
        }
    }
}

Form10.cs
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace TestForm1
{
    /// <summary>
    /// Shows how to share a BindingList<> between two forms.
    /// </summary>
    public partial class Form10 : Form
    {
        public Form10()
        {
            InitializeComponent();
            this.Init();
        }

        private void Init()
        {
            Program.masterBindingList = new BindingList<Program.MyClass>();
            this.dataGridViewMaster.DataSource = Program.masterBindingList;
        }

        private void ButtonAddRowClick(object sender, EventArgs e)
        {
            var rowIndex = this.dataGridViewMaster.RowCount;
            this.AddRow(rowIndex);
            // this.UpdateRow(rowIndex);
            this.ScrollToRow(rowIndex);
        }

        /// <summary>
        /// Add new row via the BindingList.
        /// </summary>
        private void AddRow(int rowIndex)
        {
            var myRow = new Program.MyClass { Title = "Row " + rowIndex.ToString() };
            Program.masterBindingList.Add(myRow);
        }

        private void ScrollToRow(int rowIndex)
        {
            this.dataGridViewMaster.ClearSelection();
            this.dataGridViewMaster.FirstDisplayedScrollingRowIndex = rowIndex;
            this.dataGridViewMaster.Focus();
        }

        private void buttonShow_Click(object sender, EventArgs e)
        {
            var form10b = new Form10b();
            form10b.ShowDialog();
        }
    }
}

Form10b.cs
using System.Windows.Forms;

namespace TestForm1
{
    public partial class Form10b : Form
    {
        public Form10b()
        {
            InitializeComponent();
            dataGridView1.DataSource = Program.masterBindingList;
        }
    }
}
 
Share this answer
 
v3
Comments
Abuamer 22-Dec-18 5:33am    
thank you for your concern> but could u give me an example
Abuamer 22-Dec-18 6:50am    
thank you very much. it was so helpful.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900