Grid View for ASP.NET Web Forms - How to add a column if the AutoGenerateColumns property is set to true
This example demonstrates how to add a column to the grid when the AutoGenerateColumns property is enabled.
When the AutoGenerateColumns property is set to true
, handle the DataBound event to add a column to the Grid View control. Before you add a column, check whether this column already exists to avoid duplicate columns.
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="True"
KeyFieldName="CategoryID" ondatabound="ASPxGridView1_DataBound">
</dx:ASPxGridView>
protected void ASPxGridView1_DataBound(object sender, EventArgs e) {
if (grid.Columns.IndexOf(grid.Columns["CommandColumn"]) != -1)
return;
GridViewCommandColumn col = new GridViewCommandColumn();
col.Name = "CommandColumn";
// ...
grid.Columns.Add(col);
}
- Default.aspx (VB: Default.aspx)
- Default.aspx.cs (VB: Default.aspx.vb)
(you will be redirected to DevExpress.com to submit your response)