Question

i have a DataGridView and a List<> as a DataSorce but i want show only selected columns.

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.

Was it helpful?

Solution

You can do something like this for each of the columns you want to hide:

myDataGrid.Columns[0].Visible = false;

OTHER TIPS

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:

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

You should disable the AutoGeneratedColumns property of the datagridview, create the column manually using the same column names as the property you wan't to display and then bind the datasource to it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top