0

when i creat unlimited textbox in gridview dynamically how can i access them? for example:

    int uste_uzaklik = 30;
    int nesne = ListBox1.Items.Count;
    Array.Resize(ref textboxarray, nesne * nesne);
    for (int str = 0; str < nesne; str++)
    {
        for (int stn = 0; stn < nesne; stn++)
        {
            textboxarray[idm] = new TextBox();
            textboxarray[idm].Font.Bold = true;
            textboxarray[idm].Font.Name = "Verdana";
            textboxarray[idm].ID = idm.ToString();
            textboxarray[idm].ToolTip = textboxarray[idm].ID;
            GridView2.Rows[str].Cells[stn + 1].Controls.Add(textboxarray[idm]);
            if (str == stn) textboxarray[idm].Enabled = false;
            uste_uzaklik += 30;
            idm++;
        }
    }

i add texboxes in gridview...you can imagine a matris... there is no problem... but when i access them like this:

                   if (((TextBox)(GridView2.Rows[str].Cells[stn].FindControl(idm.ToString()))).Text != null)
                    {
                        matris[i, j] = Convert.ToInt32(GridView2.Rows[str].Cells[stn].Text);
                    }

occur an error

Object reference not set to an instance of an object.

how can i solve this problem?

  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Preston Guillot Oct 31 '14 at 18:49
  • 1
    Why aren't you using textboxarray to access the TextBoxes? You created the array, why not keep reference to it? – furkle Oct 31 '14 at 18:51
  • What is `idm`??? And what is the initial and last value of `idm`??? – Nadeem Iqbal Oct 31 '14 at 18:52
  • Use `string.isnullorempty` to check, if you have blank string, and you do a null check like `!= null`, it will pass, but at that time, `Convert.ToInt32("")`, will say `Object reference not set to an instance of an object.` – Arindam Nayak Oct 31 '14 at 19:25

1 Answers1

0

References you have to controls don't cease to exist you add them to another control. You've already created an array of your TextBoxes, and you should use that to access them instead of trying to dig into the GridView in which you've added them every single time you want to change them.

Granted, you're going from a one-dimensional array of TextBoxes to a two-dimensional layout within the GridView, so you'll either have to find some way to establish how the indices match up between the two. Or, more easily, you could just turn textboxarray into a two-dimensional array and just have it exactly match the way it's laid out in the GridView. Either way, I think it'll be a lot less work than having to muck around in the GridView.

furkle
  • 5,019
  • 1
  • 15
  • 24