Printer Friendly Version      Send     
Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight
DataGrid Class
 AutoGenerateColumns Property
.NET Framework Class Library for Silverlight
DataGrid..::.AutoGenerateColumns Property
[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Gets or sets a value that indicates whether columns are created automatically when the ItemsSource property is set.

Namespace:  System.Windows.Controls
Assembly:  System.Windows.Controls.Data (in System.Windows.Controls.Data.dll)
XMLNS for XAML: Not mapped to an xmlns.

Visual Basic (Declaration)
Public Property AutoGenerateColumns As Boolean
Visual Basic (Usage)
Dim instance As DataGrid
Dim value As Boolean

value = instance.AutoGenerateColumns

instance.AutoGenerateColumns = value
C#
public bool AutoGenerateColumns { get; set; }
Visual C++
public:
property bool AutoGenerateColumns {
    bool get ();
    void set (bool value);
}
JScript
public function get AutoGenerateColumns () : boolean
public function set AutoGenerateColumns (value : boolean)
XAML Attribute Usage
<swcd:DataGrid AutoGenerateColumns="bool"/>
XAML Values
swcd:

A prefix that is defined to map the XML namespace for the System.Windows.Controls.Data assembly and the System.Windows.Controls CLR namespace.

Property Value

Type: System..::.Boolean

true if columns are generated automatically; otherwise, false. The default is false.

You can modify the Columns collection at run time regardless of whether it contains generated columns. However, if you specify columns in XAML, you should not set AutoGenerateColumns to true.

The following code example demonstrates how to set the AutoGenerateColumns and ItemsSource properties. This example is part of a larger example available in the DataGrid class overview.

XAML
<data:DataGrid x:Name="dataGrid1" 
    Height="120" Width="450" Margin="0,5,0,10"
    AutoGenerateColumns="True" />            

C#
// Autogenerate the columns, but remove the Address column.
dataGrid1.ItemsSource = Customer.GetSampleCustomerList();
dataGrid1.Columns.RemoveAt(2);            

C#
public class Customer
{
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public String Address { get; set; }
    public Boolean IsNew { get; set; }

    // A null value for IsSubscribed can indicate 
    // "no preference" or "no response".
    public Boolean? IsSubscribed { get; set; }

    public Customer(String firstName, String lastName, 
        String address, Boolean isNew, Boolean? isSubscribed)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
        this.Address = address;
        this.IsNew = isNew; 
        this.IsSubscribed = isSubscribed;
    }

    public static List<Customer> GetSampleCustomerList()
    {
        return new List<Customer>(new Customer[4] {
            new Customer("A.", "Zero", 
                "12 North Third Street, Apartment 45", 
                false, true), 
            new Customer("B.", "One", 
                "34 West Fifth Street, Apartment 67", 
                false, false),
            new Customer("C.", "Two", 
                "56 East Seventh Street, Apartment 89", 
                true, null),
            new Customer("D.", "Three", 
                "78 South Ninth Street, Apartment 10", 
                true, true)
        });
    }
}

© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker