2

I have a grid view where the columns is generated dynamically through the code [AutoGenerateColumns="false"] with one TemplateField but also I am creating another TemplateField from ASPX

How can I control the order of these two TemplateField For example, I need the TemplateField that generated from code first and then TemplateField the generated from ASPX is second as new row in the gridview.

TemplateField templateField = new TemplateField();
TemplateField uid = new TemplateField();
uid.HeaderText = "userid";
uid.ItemTemplate = new AddItemTemplate(ListItemType.Item, "userid");
GridView1.Columns.Add(uid);
for (int i = 0; i < dt.Columns.Count; i++)
{
   BoundField boundField = new BoundField();
   if (dt.Columns[i].ColumnName.ToString() != "userid")
   {
      boundField.DataField = dt.Columns[i].ColumnName.ToString();
      boundField.HeaderText = dt.Columns[i].ColumnName.ToString();
      GridView1.Columns.Add(boundField);
   }
}

aspx code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <tr>
                        <td>
                            <div id="div<%# Eval("userid") %>" >
                                <asp:GridView ID="GridView2" AllowSorting="true" >
                                    <Columns>
                                        --code--
                                    </Columns>
                                </asp:GridView>
                            </div>
                        </td>
                    </tr>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Is there any way to control this?

Xharze
  • 2,703
  • 2
  • 17
  • 30
Noora
  • 319
  • 3
  • 10
  • 26
  • i thought the order of template feild starts from the aspx code. Dynamically generated template fields index starts after assigning index to the template field in aspx code. – Waqar Janjua Jul 30 '12 at 08:11

1 Answers1

1

Instead of

GridView1.Columns.Add(boundField); 

Use:

GridView1.Columns.Insert(0, boundField);

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datacontrolfieldcollection.insert.aspx

Amiram Korach
  • 13,056
  • 3
  • 28
  • 30