0

I want to create a multiple templatefield using c#. So I am trying to create in this way.

        TemplateField[] ttfield = new TemplateField[10];

        ttfield[0].HeaderText = "Firstname";
        GridView1.Columns.Add(ttfield[0]);`

But it throws NullReferenceException.

When I try the below code, It has created two column with the same name.

        TemplateField tfield = new TemplateField();
        TemplateField[] ttfield = new TemplateField[10];

        for (int i = 0; i < splitSelectedColumn.Length; i++)
        {
            if (i < splitGroupByColumn.Length)
            {
                tfield.HeaderText = splitGroupByColumn[i];

                GridView1.Columns.Add(tfield);
            }
        }

It creates two Name column, but same name.

  • It looks like the null reference is because you didn't set `ttfield[0]` to an object before setting it's `HeaderText` (do `ttfield[0] = new TemplateField();` first), also it looks like you should create your `tfield` variable inside of the `for` loop so you are not adding a reference to the same object repeatedly. – juharr Dec 01 '16 at 13:25
  • @juharr thanks It works. – mohamed faisal Dec 01 '16 at 13:39

0 Answers0