Back to Devexpress

ASPxGridView.AutoGenerateColumns Property

aspnet-devexpress-dot-web-dot-aspxgridview-e438d325.md

latest5.5 KB
Original Source

ASPxGridView.AutoGenerateColumns Property

Gets or sets whether columns are automatically created for all fields in the underlying data source.

Namespace : DevExpress.Web

Assembly : DevExpress.Web.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
[DefaultValue(true)]
public bool AutoGenerateColumns { get; set; }
vb
<DefaultValue(True)>
Public Property AutoGenerateColumns As Boolean

Property Value

TypeDefaultDescription
Booleantrue

true to automatically create columns for all fields in the underlying data source; otherwise, false.

|

Remarks

The AutoGenerateColumns property specifies which data fields are rendered by ASPxGridView. By default, this property is set to true. This forces the grid to render each field from the data source as a column. The order of columns is the same as the order of fields in the data source.

You can also manually control which columns are to appear in the ASPxGridView control by setting the AutoGenerateColumns property to false. In this case, you should manually add columns to the ASPxGridView.Columns collection.

Example

The following example demonstrates how to add a column to the grid when the AutoGenerateColumns property is enabled.

View Example: How to add a column if the AutoGenerateColumns property is set to true

aspx
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="True" DataSourceID="ds"
    KeyFieldName="CategoryID" ondatabound="ASPxGridView1_DataBound">
</dx:ASPxGridView>
csharp
protected void ASPxGridView1_DataBound (object sender, EventArgs e) {
    ASPxGridView grid = sender as ASPxGridView;
    if (grid.Columns.IndexOf(grid.Columns["CommandColumn"]) != -1)
        return;
    GridViewCommandColumn col = new GridViewCommandColumn();
    col.Name = "CommandColumn";
    // ...
    grid.Columns.Add(col);
    // ...
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the AutoGenerateColumns property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

asp-net-web-forms-grid-create-master-detail-grid-at-runtime/CS/MasterDetailGrids/Default.aspx.cs#L29

csharp
masterGrid.ID = "masterGrid";
masterGrid.AutoGenerateColumns = false;
form1.Controls.Add(masterGrid);

asp-net-web-forms-grid-created-at-runtime-switch-data-sources/CS/Solution/Default.aspx.cs#L27

csharp
grid.Columns.Clear();
grid.AutoGenerateColumns = true;
grid.DataBind();

asp-net-web-forms-grid-switch-data-sources-at-runtime/CS/Solution/Default.aspx.cs#L26

csharp
grid.Columns.Clear();
grid.AutoGenerateColumns = true;
grid.KeyFieldName = String.Empty;

asp-net-web-forms-grid-create-master-detail-grid-at-runtime/VB/MasterDetailGrids/Default.aspx.vb#L31

vb
masterGrid.ID = "masterGrid"
masterGrid.AutoGenerateColumns = False
form1.Controls.Add(masterGrid)

asp-net-web-forms-grid-created-at-runtime-switch-data-sources/VB/Solution/Default.aspx.vb#L29

vb
grid.Columns.Clear()
grid.AutoGenerateColumns = True
grid.DataBind()

asp-net-web-forms-grid-switch-data-sources-at-runtime/VB/Solution/Default.aspx.vb#L29

vb
grid.Columns.Clear()
grid.AutoGenerateColumns = True
grid.KeyFieldName = String.Empty

See Also

ASPxGridView Class

ASPxGridView Members

DevExpress.Web Namespace