Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8514947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:00:29+00:00 2026-06-11T05:00:29+00:00

Using Windows Forms, two link labels are created dynamically. When the user clicks on

  • 0

Using Windows Forms, two link labels are created dynamically. When the user clicks on anyone of links labels, one dynamic form is created. In that form I created one data grid, a text box and a button placed dynamically (in that dynamic form). Now I want to access the dynamic data grid in the dynamic button click event. How can I do that?

private void Users_Load(object sender, EventArgs e)
{
    da = new SqlDataAdapter("Usp_Get_Employees", con);
    ds = new DataSet();
    da.Fill(ds);

    if (ds.Tables[0].Rows.Count > 0)
    {
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            string somecode = i.ToString() + ds.Tables[0].Rows[i]["eid"].ToString();
            LinkLabel  lbluser  = new LinkLabel();
            lbluser.Name =  ds.Tables[0].Rows[i]["eid"].ToString();
            lbluser.Text = ds.Tables[0].Rows[i]["ename"].ToString();
            lbluser.Location = new System.Drawing.Point(40, i * 40);
            lbluser.Size = new System.Drawing.Size(50, 30);
            Controls.Add(lbluser);
            lbluser.Click += new EventHandler(lbluser_Click);
        }
    }
}


void lbluser_Click(object sender, EventArgs e)
{
    LinkLabel lnkClis = (LinkLabel)sender;
    Form frm = new Form();
    frm.Name = lnkClis.Name;
    frm.Text = lnkClis.Text;
    frm.Show();

    DataGrid dtgrd = new DataGrid();
    dtgrd.Location = new System.Drawing.Point(10, 1 * 40);
    dtgrd.Name = lnkClis.Name;
    names = lnkClis.Name;

    TextBox tx = new TextBox();

    tx.Location = new System.Drawing.Point(10, 5 * 40);
    tx.Size = new Size(80, 30);
    tx.Multiline = true;
    tx.LostFocus += new EventHandler(tx_LostFocus);

    Button btn = new Button();
    btn.Location = new System.Drawing.Point(10, 7 * 40);
    btn.Size = new System.Drawing.Size(50, 30);
    btn.Name = lnkClis.Name;
    btn.Click += new EventHandler(btn_Click);

    frm.Controls.Add(dtgrd);
    frm.Controls.Add(tx);
    frm.Controls.Add(btn);
}

// Now I am trying to access the data grid in the btn_click event

void btn_Click(object sender, EventArgs e)
{
    Button btsave = (Button)sender;
    string eid = btsave.Name;

    object grd = btsave.Parent.Controls.Find("dtgrd", true).FirstOrDefault();

    ((DataGrid)grd).DataSource = ds.Tables[0];
}

Now I am getting an error object set of instances of an object at:

((DataGrid)grd).DataSource = ds.Tables[0];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:00:31+00:00Added an answer on June 11, 2026 at 5:00 am

    The exception message you have written:

    Now I am getting an error object set of instances of an object at

    makes no sense, but it looks like

    Object reference not set to an instance of an object

    If this is the case, I think the error lays in Find method call. According to documentation:

    Searches for controls by their Name property and builds an array of all the controls that match.

    In your button click handler you assume that grid is called dtgrd, but while you create a grid you name it like this:

    dtgrd.Name = lnkClis.Name;
    

    it will suffice if you change this line to:

    dtgrd.Name = "dtgrd";
    

    Having said that, you should consider using an anonymous method for the button click handler. It will eliminate need for calling the Find method in the first place.

     void lbluser_Click(object sender, EventArgs e)
    {
       //...
        DataGrid dtgrd = new DataGrid();
       //...
        Button btn = new Button();
       //...
        btn.Click += (sender,args)=> dtgrd.DataSource = ds.Tables[0];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been experimenting with sending messages from two .NET Windows Forms applications using
Iam using windows forms. How can I query all child controls of a Form
I have a C# project using windows forms. I need to let the user
I'm trying to change the border style of my form using Windows Forms. The
I am using standard windows forms controls under C#. I have two controls which
I am trying to validate User Input in Windows Forms Application (using MVP design
I am using two forms, where one is a rich text editor with menus
I have a windows form application with Two forms, Login and Account In the
I have an f# script using Windows Forms and I want it to run
I want to provide my colleagues with an interface (using Windows Forms or WPF)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.