Accessing form class control's reference in custom control class

OmkarHcl 206 Reputation points
2023-05-31T13:51:51.1733333+00:00

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.


  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*** 
                }
            }
        }
Developer technologies Windows Forms
Developer technologies C#
{count} votes

Accepted answer
  1. Anonymous
    2023-06-01T07:30:04.3166667+00:00

    Hi @OmkarHcl , Welcome to Microsoft Q&A.

    This is totally doable.

    What you need to do is pass target datagridview as parameter to custom datagridview.

    Put the following code into the constructor of the custom control.

    private DataGridView formDataGridView;
            public smartdatagridview(DataGridView formDataGridView)
            {
                this.formDataGridView = formDataGridView;
                InitializeComponent();
            }
    

    Then create it manually in form1.

    smartdatagridview sd = new smartdatagridview(dataGridView1);
    Controls.Add(sd);
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.