Home Reorder column in Gridview using code behind
Reply: 1

Reorder column in Gridview using code behind

Jack
1#
Jack Published in 2018-02-14 08:29:57Z

i want to switch the columns when the gridview is using ItemTemplate instead of BoundField:

 <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="lblname" runat="server" Text='name'></asp:Label>
            </ItemTemplate>
</asp:TemplateField>

- Code behind -
     if (Group == "A") { 

                ((BoundField)gv1.Columns[0]).DataField = "name";
                ((BoundField)gv1.Columns[0]).HeaderText = "Name";
                ((BoundField)gv1.Columns[1]).DataField = "gender";
                ((BoundField)gv1.Columns[1]).HeaderText = "Gender";
            }  else   { 

                ((BoundField)gv1.Columns[0]).DataField = "gender";
                ((BoundField)gv1.Columns[0]).HeaderText = "Gender";
                ((BoundField)gv1.Columns[1]).DataField = "name";
                ((BoundField)gv1.Columns[1]).HeaderText = "Name";
            }

i tried "TemplateField abc= new TemplateField()"and .add(), how to specify the new column number position?

Thanks.

ArunPratap
2#
ArunPratap Reply to 2018-02-14 08:37:43Z

i have did it and it worked for me like

Suppose you wanted to move the second column to be the first column you could do

var Move= GridView.Columns[1];
GridView.Columns.RemoveAt(1);
GridView.Columns.Insert(0, move);
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.524313 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO