Click here to Skip to main content
15,878,231 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to somehow pass the reference (from form1.cs class) and access the reference of datagridview1 which is used only in Form1.cs in my declaration of customdatagridview . The reason is that in every form of my windows application , there will be a new Datagridview (all with different names) , and their reference would be used for some purpose by this custom datagridview . Is it possible to do it . If not , what should be the alternative.


What I have tried:

public partial class smartdatagridview : DataGridView
    {
        public smartdatagridview()
        {
            InitializeComponent();
        }
        private TextBox textBox;

        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);
            CurrentCell = this[0, 0]; 
            BeginEdit(true);
        }
        protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
        {
            base.OnEditingControlShowing(e);

            if (CurrentCell.ColumnIndex == 0 && e.Control is TextBox)
            {
                textBox = (TextBox)e.Control;
                textBox.TextChanged -= Text_Changed;
                textBox.KeyDown -= Key_down;
                textBox.Enter -= Key_Enter;
                textBox.TextChanged += Text_Changed;
                textBox.KeyDown += Key_down;
                textBox.Enter +=Key_Enter;
            }
            else
            {
                textBox = null;
            }
        }

        protected override void OnCellEndEdit(DataGridViewCellEventArgs e)
        {
            base.OnCellEndEdit(e);
            if (textBox != null)
            {
                textBox.TextChanged -= Text_Changed;
                textBox.KeyDown -= Key_down;
                textBox.Enter -= Key_Enter;
                textBox = null;
            }
            if (this.CurrentCell.ColumnIndex == 0)
            {
                if (dataGridView1.Rows.Count > 0)  ***// error*** 
                {
                    this.CurrentCell.Value = dataGridView1.SelectedRows[0].Cells[1].Value.ToString(); ***// error*** 
                    dataGridView1.Visible = false;  ***// error*** 
                }
            }
        }
C#

Posted
Updated 31-May-23 6:32am

If you want to transfer data between forms, then...
Stop thinking about a datagridview object! And start thinking about the data. I'd stringly recommend to read these articles:
Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]

If you want to share the same control between forms, create your own. Here's an idea:
UserControl Class (System.Windows.Forms) | Microsoft Learn[^]
Create a Windows Forms user control with data binding - Visual Studio (Windows) | Microsoft Learn[^]
 
Share this answer
 
The Control class has a useful FindForm() method to do just what you want. It walks up the chain of Control.Parent properties until it comes to a Control which IS A Form.

There is also a Control.TopLevelControl property which in most cases will do the same thing. It is not looking for a Control which IS A Form but a Control which has Control.GetTopLevel() == true. It's behaviour differs from FindForm() in that it will step over nested forms (such as are used in a Multiple Document Interface) until it gets to the outermost form.

Alan.
 
Share this answer
 
Comments
Ankit Goel 31-May-23 10:49am    
Please suggest the code . Thanks
Alan N 31-May-23 10:56am    
Really for something as simple as this! OK but if you don't bother to do any research yourself I may not reply again.

This statement executed within your custom DataGridView will give the form on which it is located.

Form containingForm = this.FindForm();

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