Click here to Skip to main content
15,888,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to access usercontrol textbox values to parent form where it has a button and datagridview to add those textbox values

From comments:

C#
/*Main Form*/
namespace UserControlsAndDataGridView
{
    public partial class Form1 : Form
    {
        private DataTable myTable = new DataTable();
        
        public Form1()
        {
            InitializeComponent();
            //set source
            this.dataGridView1.DataSource = this.myTable;
            
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           
            
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            UserControl1 frm = new UserControl1();
            // add row
            DataRow rd = this.myTable.NewRow();

            //set value
            rd[0] = userControl11.UserName;
            rd[1] = userControl11.SurName;
            rd[2] = userControl11.ParentName;
            rd[3] = userControl11.Qualification;

            //add row to table
            this.myTable.Rows.Add(rd);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
    }
/*UserControl*/


{
    //declaring a delegate
    public delegate void myDelegateHandler(string textToString);
    
    public partial class UserControl1 : UserControl
    {
        //ParentForm pf;
        //event declaration
        public event myDelegateHandler myDelegate;
        public string UserName
        {
            get { return txtName.Text; }
            set { txtName.Text = value; }
        }
        public string SurName
        {
            get { return SurName; }
            set { SurName = value; }
        }
        public string ParentName
        {
            get { return ParentName; }
            set { ParentName = value; }
        }
        public string MobileNo
        {
            get { return MobileNo; }
            set { MobileNo = value; }
        }
        public string Qualification
        {
            get { return Qualification; }
            set { Qualification = value; }
        }
        public UserControl1()
        {
            InitializeComponent();
        }
        //public UserControl1(Form1 Parent)
        //{
        //    InitializeComponent();
        //    pf = new ParentForm();
        //    pf = Parent;

        //}

        private void UserControl1_Load(object sender, EventArgs e)
        {

        }


        
    }
}

/* designer*/
 partial class Form1
    {
        /// 

        /// Required designer variable.
        /// 

        private System.ComponentModel.IContainer components = null;

        /// 

        /// Clean up any resources being used.
        /// 

        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// 

        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 

        private void InitializeComponent()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.Column5 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.btnAdd = new System.Windows.Forms.Button();
            this.userControl11 = new UserControlsAndDataGridView.UserControl1();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.Sus


and
and usercontrol designer
C#
private System.Windows.Forms.Label lblUserControls;
       private System.Windows.Forms.Label label1;
       private System.Windows.Forms.TextBox txtQualification;
       private System.Windows.Forms.TextBox txtMobileNo;
       private System.Windows.Forms.TextBox txtSurName;
       private System.Windows.Forms.TextBox txtParentName;
       private System.Windows.Forms.TextBox txtName;
       private System.Windows.Forms.Label lblQualification;
       private System.Windows.Forms.Label lblMobileNo;
       private System.Windows.Forms.Label lblParentName;
       private System.Windows.Forms.Label lblSurName;
       private System.Windows.Forms.Label lblName;
Posted
Updated 16-May-12 12:06pm
v3

1 solution

Exposing them via Properties is the normal way:
C#
/// <summary>
/// Gets and sets the value of the input.
/// </summary>
public string Value
    {
    get { return tbValue.Text; }
    set { tbValue.Text = value; }
    }
/// <summary>
/// Gets and set the value of the prompt.
/// </summary>
public string Prompt
    {
    get { return labPrompt.Text; }
    set { labPrompt.Text = value; }
    }
The parent form then just accesses teh properties when it needs the information.
 
Share this answer
 
Comments
Member 8983639 16-May-12 4:56am    
ya i have set the properties in the usercontrol.cs file even then unable to add the textbox values to grid

// add row
DataRow rd = this.myTable.NewRow();

//set value
rd[0] = userControl11.Naame;
rd[1] = userControl11.SurName;
rd[2] = userControl11.ParentName;
rd[3] = userControl11.Qualification;

//add row to table
this.myTable.Rows.Add(rd);
Member 8983639 16-May-12 5:00am    
thanks alot.........:) for the reply and my doubt is like i have to add those usercontrol text values to datagridview on button click which is on main form
OriginalGriff 16-May-12 5:07am    
As long as your datagridview isn't databound, you should be able to do it with pretty much the code you have.
What problem are you having with it?
Member 8983639 16-May-12 5:31am    
when i click on the button on main form after entering the textbox values name,surname,parentname,qualification theer is an error saying that the loop is running infinite times .................... am a very beginner in .net dnt knw how to use delegates to get the textbox values from child form(usercontrol) texboxes
OriginalGriff 16-May-12 5:37am    
What loop?
Why are you using delegates for this at all?

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