我想在gridview使用ItemTemplate而不是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";
}
我试过“TemplateField abc = new TemplateField()”和.add(),如何指定新的列号位置?
感谢。
答案 0 :(得分:0)
我已经做过,它对我有用
假设您想将第二列移动为您可以执行的第一列
var Move= GridView.Columns[1];
GridView.Columns.RemoveAt(1);
GridView.Columns.Insert(0, move);