2

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];

4 Answers 4

2
+50

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];
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much i got it. thank you for giving response Mr. Rafal
1

Try the following code

    public Form1()
    {
        Form f1 = new Form();

        f1.Text = "New Form";

        TextBox t1 = new TextBox();
        t1.Top = 0;
        t1.Name = "t1";
        t1.Visible = true;
        f1.Controls.Add(t1);

        Button b1 = new Button();
        b1.Top = 30;
        b1.Name = "b1";
        b1.Text = "Click";
        b1.Click += b1_Click;
        f1.Controls.Add(b1);

        f1.Show();
    }

    public void b1_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        object txt = btn.Parent.Controls.Find("t1", false).First();
        ((TextBox)txt).Text = "Hi, you have clicked me.";
    }

1 Comment

hi nithesh, in the above case working fine , but when i take datagrid it is showing error. My code is : object grd = btsave.Parent.Controls.Find("dtgrd", false).FirstOrDefault(); ((DataGrid)grd).DataSource = ds.Tables[0];
1

I modified Nitesh's code a bit. Just capture the textbox in the click handler using a lambda:

public Form1()
{
    Form f1 = new Form();

    f1.Text = "New Form";

    TextBox t1 = new TextBox();
    t1.Top = 0;
    t1.Name = "t1";
    t1.Visible = true;
    f1.Controls.Add(t1);

    Button b1 = new Button();
    b1.Top = 30;
    b1.Name = "b1";
    b1.Text = "Click";
    b1.Click += (sender, args) => MessageBox.Show("The text is: " + t1.Text);
    f1.Controls.Add(b1);

    f1.Show();
}

Comments

0

The error you are getting is from the statement (as the grd object is null):

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

Since you are trying to catch hold of a dynamic control, it's good have a proper null checks, type checks, and error handling. Something like this:

if(grd != null && grd is DataGrid)
    ((DataGrid)grd).DataSource = ds.Tables[0];

1 Comment

please add formatting to your code, it will make it more readable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.