[英]How can I dynamically change the order of my GridView columns in WPF?
I have two radiobuttons.我有两个单选按钮。 If I click the first radiobutton I want the column's order to be:
如果我单击第一个单选按钮,我希望列的顺序为:
If I click the second radio button I want the column's order to be:如果我单击第二个单选按钮,我希望列的顺序为:
Here is a sample of my XAML:这是我的 XAML 的示例:
<asp:GridView ID="dgAssets" runat="server" AutoGenerateColumns="False" AllowPaging="True"
DataKeyNames="ID" AllowSorting="True" OnPageIndexChanging="dgAssets_PageIndexChanging"
Width="100%" OnRowCommand="dgAssets_RowCommand" OnRowDataBound="dgAssets_RowDataBound"
OnSorting="dgAssets_Sorting">
<Columns>
<asp:TemplateField Visible="False">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "ID")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="ASSETNAME">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "ASSETNAME")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="ASSET">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "ASSET")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="GROUPS">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "GROUPS")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="TYPENAME">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "TYPENAME")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="IPRISKLEVEL">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "IPRISKLEVEL")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Instead of adding the columns in design mode, it might be an idea to add them dynamically using C# code.与其在设计模式下添加列,不如使用 C# 代码动态添加它们。 Which will allow you to order them any way you like.
这将允许您以任何您喜欢的方式订购它们。
Code for dynamically adding columns:动态添加列的代码:
DataTable dt = yourDataTable;
foreach (DataColumn col in dt.Columns)
{
BoundField bfield = new BoundField();
bfield.DataField = col.ColumnName;
bfield.HeaderText = col.ColumnName;
dgAssets.Columns.Add(bfield);
}
If you only want to sort on the radio button click then use the DataView.Sort method eg如果您只想对单选按钮单击进行排序,请使用 DataView.Sort 方法,例如
dataView.Sort = "AssetName, Asset, Groups, TypeName, Iprisklevel";
Then on the second radio button click do然后在第二个单选按钮上单击执行
dataView.Sort = "Groups, AssetName, Asset, TypeName, Iprisklevel";
You will have to add your columns dynamically.您必须动态添加列。 Here is some sample code that add a column that is in turn databound.
下面是一些示例代码,它们添加了一个反过来数据绑定的列。 Note that this Template is a sub class of your page class.
请注意,此模板是您的页面 class 的子 class。
This approach uses a fully templated column.此方法使用完全模板化的列。 I believe you can also use Bound Columns which might be a bit simpler.
我相信您也可以使用可能更简单的绑定列。 All depends on what you typically use in your grid view.
一切都取决于您通常在网格视图中使用的内容。
Not tested but cut from working code:未经测试但从工作代码中删除:
private void AddTemplates()
{
TemplateField templateField = new TemplateField();
templateField.HeaderText = entity.ChangeHistoryColumn;
templateField.ItemTemplate = new GridViewColumnTemplate();
GridViewMain.Columns.Add(templateField);
}
public class GridViewColumnTemplate : ITemplate
{
public GridViewColumnTemplate() { }
public void InstantiateIn(Control container)
{
Label label = new Label();
label.DataBinding += delegate(object sender, EventArgs e)
{
GridViewRow row = (GridViewRow)label.NamingContainer;
int headerID = (int)DataBinder.Eval(row.DataItem, "HeaderID");
((Label)sender).Text = headerID.ToString();
};
container.Controls.Add(label);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.