ComponentOne DataGrid for WPF and Silverlight
DataGrid for WPF and Silverlight Overview / Selection Mode / Customizing Automatically Generated Columns
In This Topic
    Customizing Automatically Generated Columns
    In This Topic

    You can customize columns even if columns are automatically generated. If the AutoGenerateColumns property is set to True and columns are automatically generated, you can customize how generated columns are displayed in code by handling the C1DataGrid.AutoGeneratingColumn event.

    Adding the AutoGeneratingColumn Event Handler

    Complete the following steps to add the AutoGeneratingColumn event handler:

    1. Switch to Code view and add an event handler for the AutoGeneratingColumn event, for example:

      Visual Basic
      Copy Code
      Private Sub C1DataGrid1_AutoGeneratingColumn(ByVal sender As System.Object, ByVal e As C1.WPF.DataGrid.DataGridAutoGeneratingColumnEventArgs) Handles C1DataGrid1.AutoGeneratingColumn                
        ' Add code here.                
      End Sub
      
      C#
      Copy Code
      private void C1DataGrid1_AutoGeneratingColumn(object sender, C1.WPF.DataGrid.DataGridAutoGeneratingColumnEventArgs e)        
          {                 
               // Add code here.
          }
      

    2. Switch to Source view and add the event handler to instances of the C1DataGrid control, for example:

    <c1:C1DataGrid x:Name="c1DataGrid1" AutoGenerateColumns="True" AutoGeneratingColumn=" c1DataGrid1_AutoGeneratingColumn"></c1:C1DataGrid>

     

    You can now add code to the AutoGeneratingColumn event handler to customize the appearance and behavior of automatically generated columns. Below are examples of customizing column formatting and behavior.

    Canceling Column Generation

    You can cancel the generation of specific columns in the AutoGeneratingColumn event. For example, you can use the following code to cancel the generation of Boolean columns in the grid:

    Visual Basic
    Copy Code
    <c1:C1DataGrid x:Name="c1DataGrid1" AutoGenerateColumns="True" AutoGeneratingColumn=" Private Sub C1DataGrid1_AutoGeneratingColumn(ByVal sender As System.Object, ByVal e As C1.WPF.DataGrid.DataGridAutoGeneratingColumnEventArgs) Handles C1DataGrid1.AutoGeneratingColumn
    ' Cancel automatic generation of all Boolean columns.
        If e.Property.PropertyType Is GetType(Boolean) Then
            e.Cancel = True
        End If
    End Sub
    
    C#
    Copy Code
    private void c1DataGrid1_AutoGeneratingColumn(object sender, C1.WPF.DataGrid.DataGridAutoGeneratingColumnEventArgs e)
    {
        // Cancel automatic generation of all Boolean columns.
        if (e.Property.PropertyType == typeof(bool))
            e.Cancel = true;
    }
    


    Changing a Column Header

    In the AutoGeneratingColumn event you can change the text that appears in the header of automatically generated columns. For example, you can change the "ProductName" column so that it appears with the "Name" header using the following code:

    Visual Basic
    Copy Code
    Private Sub C1DataGrid1_AutoGeneratingColumn(ByVal sender As System.Object, ByVal e As C1.WPF.DataGrid.DataGridAutoGeneratingColumnEventArgs) Handles C1DataGrid1.AutoGeneratingColumn
        ' Modify the header of the ProductName column.
        If e.Column.Header.ToString() = "ProductName" Then
             e.Header = "Name"
        End If
    End Sub
    
    C#
    Copy Code
    private void c1DataGrid1_AutoGeneratingColumn(object sender, C1.WPF.DataGrid.DataGridAutoGeneratingColumnEventArgs e)
    {
        // Modify the header of the ProductName column.
        if (e.Column.Header.ToString() == "ProductName")
            e.Column.Header = "Name";
    }
    

    Preventing Column Interaction

    Using the AutoGeneratingColumn event you can change how end users interact with specific generated columns. For example, you can prevent users from moving read-only columns with the following code:

    Visual Basic
    Copy Code
    Private Sub C1DataGrid1_AutoGeneratingColumn(ByVal sender As System.Object, ByVal e As C1.WPF.DataGrid.DataGridAutoGeneratingColumnEventArgs) Handles C1DataGrid1.AutoGeneratingColumn
        ' Modify the header of the ProductName column.
        If e.Column.IsReadOnly = True Then
           e.Column.CanUserMove = False
        End If
    End Sub
    
    C#
    Copy Code
    private void c1DataGrid1_AutoGeneratingColumn(object sender, C1.WPF.DataGrid.DataGridAutoGeneratingColumnEventArgs e)
    {
        // Modify the header of the ProductName column.
        if (e.Column.IsReadOnly == true)
            e.Column.CanUserMove = false;
    }