Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (152).NET Core  (26)Angular  (49)ASP.NET  (48)ASP.NET Core  (64)ASP.NET MVC  (86)Azure  (29)Blazor  (107)DocIO  (20)Essential JS 2  (72)Essential Studio  (164)File Formats  (20)Flutter  (115)JavaScript  (150)Microsoft  (102)PDF  (59)React  (33)Succinctly series  (124)Syncfusion  (490)TypeScript  (30)Uno Platform  (3)UWP  (4)Vue  (26)Webinar  (27)Windows Forms  (55)WinUI  (27)WPF  (123)Xamarin  (132)XlsIO  (24)Other CategoriesBarcode  (4)BI  (29)Bold BI  (4)Build conference  (6)Business intelligence  (53)Button  (4)C#  (108)Chart  (53)Cloud  (10)Company  (445)Dashboard  (6)Data Science  (3)Data Validation  (5)DataGrid  (50)Development  (263)Doc  (7)DockingManager  (1)eBook  (94)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (20)Extensions  (15)File Manager  (4)Gantt  (9)Gauge  (7)Git  (4)Grid  (27)HTML  (9)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (233)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (25)Performance  (7)PHP  (1)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (6)Road Map  (9)Scheduler  (29)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (19)Solution Services  (2)Spreadsheet  (10)SQL  (7)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (11)Tips and Tricks  (65)UI  (139)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (1)Visual Studio  (19)Visual Studio Code  (13)Web  (227)What's new  (146)Windows 8  (19)Windows App  (1)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Share on twitter
Share on facebook
Share on linkedin
Exploring Column Types in WinUI 3 DataGrid

Exploring Column Types in WinUI 3 DataGrid

The WinUI DataGrid is used for efficiently displaying and manipulating tabular data. Its rich feature set includes data binding, editing, sorting, filtering, grouping, and data virtualization.

It provides various built-in columns to easily edit the values in a cell. While editing a cell, an editor will be loaded based on the data type of its column. DataGrid columns are automatically generated based on the type of data object bound to the DataGrid.

The following table shows the data type and its corresponding column type.

Data TypeColumn
string, object, dynamicGridTextColumn
double and it’s nullableGridNumericColumn
DateTimeOffset and its nullableGridDateColumn
Uri, Uri?GridHyperLinkColumn
bool, bool?GridCheckBoxColumn

You can also disable the AutoGenerateColumns property in the WinUI DataGrid and manually create columns instead. The following code demonstrates this.

<dataGrid:SfDataGrid AutoGenerateColumns="False"
                ItemsSource="{Binding Employees}">
    <dataGrid:SfDataGrid.Columns>
        <dataGrid:GridCheckBoxColumn MappingName="EmployeeStatus" />
        <dataGrid:GridNumericColumn MappingName="EmployeeID"/>
        <dataGrid:GridTextColumn MappingName="Name" />
        <dataGrid:GridTextColumn MappingName="Gender" />
        <dataGrid:GridDateColumn MappingName="HireDate" />        
    </dataGrid:SfDataGrid.Columns>
</dataGrid:SfDataGrid>

In this blog, we are going to see how to use the different column types in the WinUI DataGrid with code examples.

Types of columns

The following are the built-in column types in the WinUI DataGrid. Each column has properties to handle different types of data:

Text column

The text column holds textual content in its cells. The framework TextBox control is loaded as the editor to edit the cell values.

Refer to the following code example.

<dataGrid:SfDataGrid AutoGenerateColumns="False"
                ItemsSource="{Binding Employees}">
    <dataGrid:SfDataGrid.Columns>
        ……
        <dataGrid:GridTextColumn HeaderText="Employee Name" MappingName="Name" />
        <dataGrid:GridTextColumn MappingName="Gender" />
        ……
    </dataGrid:SfDataGrid.Columns>
</dataGrid:SfDataGrid>
Text Column in WinUI DataGrid
Text Column in WinUI DataGrid

Note: For more information, refer to the GridTextColumn documentation.

Numeric column

The numeric column displays numeric values in its cells. The Syncfusion SfNumberBox control is loaded as the editor to edit the cell values.

The numeric column supports formatting the values using the DisplayNumberFormat or NumberFormatter. You can show the values in currency, decimal, and percentage formats.

Refer to the following code example.

<dataGrid:SfDataGrid
                AllowEditing="True"
                AutoGenerateColumns="False"
                ItemsSource="{Binding Employees}">
    <dataGrid:SfDataGrid.Columns>
        ……
        <dataGrid:GridNumericColumn HeaderText="Employee ID" MappingName="EmployeeID" TextAlignment="Right" />
        ……
    </dataGrid:SfDataGrid.Columns>
</dataGrid:SfDataGrid>
Numeric Column in WinUI DataGrid
Numeric Column in WinUI DataGrid

Note: For more information, refer to the GridNumericColumn documentation.

Date column

The date column shows date values in its cells. The Syncfusion SfCalanderDatePicker control is loaded as the editor to edit the cell values.

The date column supports formatting the values using the  DisplayDateFormat property. Using this, you can show the values in date, month, and year formats.

Refer to the following code example.

<dataGrid:SfDataGrid
                AllowEditing="True"
                AutoGenerateColumns="False"
                ItemsSource="{Binding Employees}">
    <dataGrid:SfDataGrid.Columns>
        ……

        <dataGrid:GridDateColumn MappingName="HireDate"
                        HeaderText="Hire Date"
                        TextAlignment="Right" />
        ……
    </dataGrid:SfDataGrid.Columns>
</dataGrid:SfDataGrid>
Date Column in WinUI DataGrid
Date Column in WinUI DataGrid

Note: For more details, refer to the GridDateColumn documentation.

Time column

The time column holds a time value in its cells. The Syncfusion SfTimePicker control is loaded as the editor to edit the cell values.

The time column supports formatting the values using the DisplayTimeFormat property. With this, you can show the time in hours, minutes, seconds, meridian, 12-Hour, and 24-Hour formats.

Note: This is not an autogenerated column. You can create the column manually and add it to the DataGrid.

<dataGrid:SfDataGrid
                AllowEditing="True"
                AutoGenerateColumns="False"
                ItemsSource="{Binding Employees}">
    <dataGrid:SfDataGrid.Columns>
        ……
        <dataGrid:GridTimeColumn MappingName="CheckInTime" HeaderText="Check In Time" 
                                 AllowNull="True" 
                                 DisplayTimeFormat="HH:mm:ss" 
                                 EditMode="Mask" 
                                 PlaceholderText="Enter a Time" 
                                 TextAlignment="Right"/>
        ……
    </dataGrid:SfDataGrid.Columns>
Time Column in WinUI DataGrid
Time Column in WinUI DataGrid

Note: For more details, refer to the GridTimeColumn documentation.

Combo box column

The combo box column holds the IEnumerable type value and displays the selected value in its cells. The framework ComboBox control is loaded as the editor to select and change the values in the cell. You can set the data source of the ComboBox using the GridComboBoxColumn.ItemsSource property. By default, the GridComboBoxColumn displays the value using the MappingName property.

Refer to the following code example.

<dataGrid:SfDataGrid
                AllowEditing="True"
                AutoGenerateColumns="False"
                ItemsSource="{Binding Employees}">
    <dataGrid:SfDataGrid.Columns>
            ……
        <dataGrid:GridComboBoxColumn HeaderText="Designation"  
                                     MappingName="Title" 
                                     ItemsSource="{Binding TitleCollection, Source={StaticResource employeeViewModel}}" />
             ……
    </dataGrid:SfDataGrid.Columns>
</dataGrid:SfDataGrid>
ComboBox Column in WinUI DataGrid
ComboBox Column in WinUI DataGrid

Note: For more details, refer to the GridComboBoxColumn documentation.

Checkbox column

The check box column holds Boolean values in its cells. Here, we will load the framework CheckBox control as the editor to edit the cell values.

By default, the checked and unchecked states represent a true or false value, respectively. If you set the IsThreeState property to true, the checkbox will have an intermediate state. This will work when you set the type of the property to nullable Boolean.

Refer to the following code example.

<dataGrid:SfDataGrid AutoGenerateColumns="False"
                AllowEditing="True"
                ItemsSource="{Binding Employees}">
    <dataGrid:SfDataGrid.Columns>
            ……
        <dataGrid:GridCheckBoxColumn HeaderText="Availability" MappingName="EmployeeStatus"/>
            ……
    </dataGrid:SfDataGrid.Columns>
</dataGrid:SfDataGrid>
Checkbox Column in WinUI DataGrid
Checkbox Column in WinUI DataGrid

Note: For more details, refer to the GridCheckBoxColumn documentation.

The hyperlink column holds the URI data in its cells. Here, we will load the framework HyperlinkButton control as the editor to edit the cell values. You can navigate to the corresponding URI using the CurrentCellRequestNavigate event in the DataGrid.

Refer to the following code example.

<dataGrid:SfDataGrid
                 AllowEditing="True"
                 AutoGenerateColumns="False"
                 ItemsSource="{Binding Employees}">
     <dataGrid:SfDataGrid.Columns>
            ……
         <dataGrid:GridHyperlinkColumn MappingName="City"/>
            ……         
     </dataGrid:SfDataGrid.Columns>
 </dataGrid:SfDataGrid>
Hyperlink Column in WinUI DataGrid
Hyperlink Column in WinUI DataGrid

Note: For more details, refer to the GridHyperlinkColumn documentation.

Image column

This image column holds the image in its cells. The framework Image control is loaded to display the grid cell content.

Note: This is not an autogenerated column. You can create the column manually and add it to the DataGrid.

<dataGrid:SfDataGrid x:Name="dataGrid"
               AutoGenerateColumns="False" 
               ItemsSource="{Binding Orders}">
    <dataGrid:SfDataGrid.Columns>
            ……
        <dataGrid:GridImageColumn MappingName="ImageLink"
                            HeaderText="Flag"
                            Stretch="Uniform"
                            TextAlignment="Center" />
            ……
    </dataGrid:SfDataGrid.Columns>
</dataGrid:SfDataGrid>
Image Column in WinUI DataGrid
Image Column in WinUI DataGrid

Note: Refer to the GridImageColumn documentation for more details.

Toggle switch column

This toggle switch column holds Boolean value in its cells. We will load the framework ToggleSwitch control to display the cell values. With this, you can display both text and toggle switches.

Note: This is not an autogenerated column. You can create the column manually and add it to the DataGrid.

<dataGrid:SfDataGrid
                AllowEditing="True"
                AutoGenerateColumns="False"
                ItemsSource="{Binding Employees}">
    <dataGrid:SfDataGrid.Columns>
            ……
        <dataGrid:GridToggleSwitchColumn HeaderText="Availability"
                                         MappingName="EmployeeStatus" 
                                         AllowEditing="True"/>
            ……
    </dataGrid:SfDataGrid.Columns>
</dataGrid:SfDataGrid>
Toggle Switch Column in WinUI DataGrid
Toggle Switch Column in WinUI DataGrid

Note: Refer to the GridToggleSwitchColumn documentation for more information.

Template column

This column displays template-specified cell content. You can load any kind of WinUI control in the display and edit modes for all columns by setting the CellTemplate and EditTemplate properties, respectively.

By default, the underlying record is DataContext for CellTemplate. So, you have to define the template for each column to display values based on the MappingName property.

Refer to the following code example.

<dataGrid:SfDataGrid AutoGenerateColumns="False"
                       AllowEditing="True"
                       ItemsSource="{Binding Orders}">
    <dataGrid:SfDataGrid.Columns>
            ……
        <dataGrid:GridTemplateColumn MappingName="Name" HeaderText="Employee Name" >
            <dataGrid:GridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" />
                </DataTemplate>
            </dataGrid:GridTemplateColumn.CellTemplate>
            <dataGrid:GridTemplateColumn.EditTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding CustomerName, Mode=TwoWay}" FontStyle="Italic" FontWeight="Bold" />
                </DataTemplate>
            </dataGrid:GridTemplateColumn.EditTemplate>
        </dataGrid:GridTemplateColumn>
            ……
    </dataGrid:SfDataGrid>
Template Column in WinUI DataGrid
Template Column in WinUI DataGrid

Note: Refer to the GridTemplateColumn documentation for more information.

Unbound column

The WinUI DataGrid also allows you to add additional columns that are not bound with the data object from the underlying data source. You can add an unbound column using the GridUnboundColumn class. The data for an unbound column is populated by setting the Expression or Format property or through the QueryUnboundColumnValue event.

Refer to the following code.

<dataGrid:SfDataGrid
                AllowEditing="True"
                AutoGenerateColumns="False"
                ItemsSource="{Binding Employees}">
    <dataGrid:SfDataGrid.Columns>
            ……
        <dataGrid:GridUnboundColumn MappingName="CurrentSalary" 
                                    Expression="Salary*Rating/100" 
                                    HeaderText="Hike Salary" 
                                    TextAlignment="Right" />
            ……
    </dataGrid:SfDataGrid.Columns>
</dataGrid:SfDataGrid>
Unbound Column in WinUI DataGrid
Unbound Column in WinUI DataGrid

Note: Refer to the Unbound Column in the WinUI DataGrid for more information.

Conclusion

Thanks for reading! I hope you now have enough information about the various column types in the WinUI DataGrid. Try out these column types and leave your feedback in the comments section below!

Browse our WinUI documentation for complete details about our WinUI controls. Also, don’t miss our demo app in the Microsoft Store.

For current customers, the new version is available for download from the License and Downloads page. If you are not yet a Syncfusion customer, you can try our 30-day free trial to check out our newest features.

Also, you can contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Related blogs

Tags:

Share this post:

Share on twitter
Share on facebook
Share on linkedin
Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top