Upravit

Sdílet prostřednictvím


DataGrid.AutoGenerateColumns Property

Definition

Gets or sets a value that indicates whether BoundColumn objects are automatically created and displayed in the DataGrid control for each field in the data source.

public:
 virtual property bool AutoGenerateColumns { bool get(); void set(bool value); };
public virtual bool AutoGenerateColumns { get; set; }
member this.AutoGenerateColumns : bool with get, set
Public Overridable Property AutoGenerateColumns As Boolean

Property Value

true if BoundColumn objects are automatically created and displayed; otherwise, false. The default value is true.

Examples

The following code example demonstrates how to use the AutoGenerateColumns property to automatically create a BoundColumn object for each field in the data source.

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

   <script language="C#" runat="server">
 
      ICollection CreateDataSource() 
      {
         DataTable dt = new DataTable();
         DataRow dr;
 
         dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
         dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
         dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
 
         for (int i = 0; i < 9; i++) 
         {
            dr = dt.NewRow();
 
            dr[0] = i;
            dr[1] = "Item " + i.ToString();
            dr[2] = 1.23 * (i + 1);
   
            dt.Rows.Add(dr);
         }
 
         DataView dv = new DataView(dt);
         return dv;
      }
   
      void Page_Load(Object sender, EventArgs e) 
      {
   
         if (!IsPostBack) 
         {
            // Need to load this data only once.
            ItemsGrid.DataSource= CreateDataSource();
            ItemsGrid.DataBind();
         }
      }
 
   </script>
 
<head runat="server">
    <title>DataGrid AutoGenerateColumns Example</title>
</head>
<body>
 
   <form id="form1" runat="server">
 
      <h3>DataGrid AutoGenerateColumns Example</h3>
 
      <b>Product List</b>
 
      <asp:DataGrid id="ItemsGrid"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           AutoGenerateColumns="true"
           runat="server">

         <HeaderStyle BackColor="#00aaaa">
         </HeaderStyle>

      </asp:DataGrid>
 
   </form>
 
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

   <script language="VB" runat="server">
     Function CreateDataSource() As ICollection
        Dim dt As New DataTable()
        Dim dr As DataRow
        
        dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
        dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
        dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
        
        Dim i As Integer
        For i = 0 To 8
            dr = dt.NewRow()
            
            dr(0) = i
            dr(1) = "Item " & i.ToString()
            dr(2) = 1.23 *(i + 1)
            
            dt.Rows.Add(dr)
        Next i
        
        Dim dv As New DataView(dt)
        Return dv
    End Function 'CreateDataSource


    Sub Page_Load(sender As Object, e As EventArgs)
        
        If Not IsPostBack Then
            ' Need to load this data only once.
            ItemsGrid.DataSource = CreateDataSource()
            ItemsGrid.DataBind()
        End If
    End Sub 'Page_Load
 
   </script>
 
<head runat="server">
    <title>DataGrid AutoGenerateColumns Example</title>
</head>
<body>
 
   <form id="form1" runat="server">
 
      <h3>DataGrid AutoGenerateColumns Example</h3>
 
      <b>Product List</b>
 
      <asp:DataGrid id="ItemsGrid"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           AutoGenerateColumns="true"
           runat="server">

         <HeaderStyle BackColor="#00aaaa">
         </HeaderStyle>

      </asp:DataGrid>
 
   </form>
 
</body>
</html>

Remarks

Use this property to automatically create a BoundColumn object for each field in the data source. Each field is then rendered as a column in the DataGrid control in the order that the fields appear in the data source.

Not all data types can be bound to the DataGrid control. If a field contains an unsupported data type, a column is not created for that field. If the data source only contains one column with an unsupported data type, an exception is thrown. The following table shows the data types that can be bound to the control.

Data Type Description
Primitives A primitive data type, such as System.Int32, Char, Double, and so on. For a complete list, see Type.IsPrimitive.
String A System.String object.
DateTime A System.DateTime object.
Decimal A System.Decimal object.

Note

Explicitly declared columns may be used in conjunction with auto-generated columns. When using both, explicitly declared columns will be rendered first, followed by the auto-generated columns. Auto-generated columns are not added to the Columns collection.

Applies to

See also