简体   繁体   English

如何指定列表中的列以显示在DataSource GridView中? C#

[英]How Specify columns at list to show in DataSource GridView? C#

i have a DataGridView and a List<> as a DataSorce but i want show only selected columns. 我有一个DataGridView和一个List <>作为DataSorce,但我只想显示选定的列。

i have something like this... (only for example) 我有这样的事情...(仅作为示例)

 class Order{
    public int IdOrder;
    public string Product;
    public double Price;
    public int quantity;
 }

 List<Order> orders = new List<Order>();
 myDataGrid.DataSource = orders;

note: additional i use devexpress components and xPO framework(it's the same) Thanks. 注意:另外,我使用devexpress组件和xPO框架(相同)谢谢。

您可以对要隐藏的每个列执行以下操作:

myDataGrid.Columns[0].Visible = false;

After you have set the DataSource and populated the columns, you could iterate through the generated columns and hide/remove some of them: 设置数据源并填充列之后,可以遍历生成的列并隐藏/删除其中的一些列:

foreach (var column in gridView1.Columns.OfType<DevExpress.XtraGrid.Columns.GridColumn>())
{
    if (column.FieldName == "MemberToBeHidden")
    {
        // Just hide it by default, user can still choose to show it later
        column.Visible = false;

        // Or completely remove from the grid
        gridView1.Columns.Remove(column);
    }
}

Or if you don't want the field to appear at all, you could mark it with BrowsableAttribute : 或者,如果您根本不想显示该字段,则可以使用BrowsableAttribute对其进行标记:

class Order
{
    public int IdOrder;
    public string Product;
    public double Price;
    public int quantity;
    [Browsable(false)]
    public string MemberToBeHidden;
}

您应该禁用datagridview的AutoGeneratedColumns属性,使用与不想显示的属性相同的列名手动创建列,然后将数据源绑定到该列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM