1024programmer Asp.Net WPF introductory tutorial series twenty-five – DataGrid usage example (2)

WPF introductory tutorial series twenty-five – DataGrid usage example (2)

WPF introductory tutorial series twenty-five – DataGrid usage example (2)

The main feature of WPF technology is data-driven UI, so the development process using WPF technology is centered on data. WPF provides a data binding mechanism. When data changes, WPF will automatically send notifications to update the UI. Today we will learn about the DataGrid in WPF in .NET 7. The DataGrid is a very widely used control and is frequently used in web development in Asp.Net or WinForm (WPF) application development. Various data can be displayed flexibly, conveniently and effectively through the data form DataGrid. I looked at the DataGrid example I wrote before. This example is a bit simple. It does not use the Command instruction and does not use the MVVM mode. It seems to be somewhat lacking now. We are going to improve this DataGrid example and apply the Command instruction and MVVM pattern in the example.

WPF introductory tutorial series directory
WPF introductory tutorial series two— —Introduction to Application

WPF introductory tutorial series three – Introduction to Application (continued)

WPF introductory tutorial series four— —Introduction to Dispatcher

WPF introductory tutorial series five – Introduction to Window

WPF introductory tutorial series 11 ——Dependency attributes (1)
WPF introductory tutorial series fifteen ——Data binding in WPF (1)

3. Types of DataGrid columns

By default, when we set the ItemSource property for the DataGrid control, the DataGrid will automatically generate corresponding columns according to the data type. Each column in the data table Each row is bound to an object in the data source, and each column in the data table is bound to a property of the data object. If you want the DataGrid data in the user interface to automatically update when data is added or deleted from the data source, the DataGrid’s bound data source must implement the interface INotifyCollectionChanged, such as ObservableCollection.

It has been more than ten years since WPF was launched. There are still four types of columns in the DataGrid in WPF. The following table lists the four columns and their data types supported by DataGrid.

Column type

Show data

Data type

DataGridHyperlinkColumn

Use display URI data.

URI

DataGridComboBoxColumn

Use to display enumeration data and other data that require drop-down box selection.

Enum,String

DataGridTextColumn

Use display text

String

DataGridCheckBoxColumn

Use to display Boolean data

Bool</BindGrid();
}
}
}

8. Press the F5 key in Visual Studio 2022 to start the WPF application, and then use the left mouse button to click the “Refresh” button. As shown below. From the picture, the data is bound to the DataGrid, but it is not displayed in the DataGrid.

9. This is because we do not use automatic column generation and do not bind data to specific columns. Next we’ll bind the data.

 <DataGrid x:Name="gridArea" Grid.Row="1" d:ItemsSource="{d:SampleData ItemCount=5}" 
AutoGenerateColumns
="False" HorizontalAlignment="Left" VerticalAlignment ="Top" > <DataGrid.Columns > <DataGridComboBoxColumn Header="City" Width="120" /> <DataGridTextColumn Header="County, District and Town" Width="*" Binding="{Binding Name}" ClipboardContentBinding="{x:Null}"/> <DataGridTextColumn Header="Postcode" Width="100" Binding="{Binding Code}" ClipboardContentBinding="{x:Null}"/> <DataGridTextColumn Header="Creation Time" Width="160" Binding=" {Binding Created}" ClipboardContentBinding=" {x:Null}"/> <DataGridTextColumn Header="Update Time" Width="160" Binding=" {Binding Updated}" ClipboardContentBinding=" {x:Null}"/> </DataGrid.Columns> </DataGrid>

10. Press the F5 key in Visual Studio 2022 to start the WPF application, and then use the left mouse button to click the “Refresh” button. As shown below.

0, 255, 1)”>=”Update time” Width=”160″ Binding=”{Binding Updated} ClipboardContentBinding=”{x:Null}/>

</DataGrid.Columns>
</DataGrid>

10. Press the F5 key in Visual Studio 2022 to start the WPF application, and then use the left mouse button to click the “Refresh” button. As shown below.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/wpf-introductory-tutorial-series-twenty-five-datagrid-usage-example-2/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

The latest and most comprehensive programming knowledge, all in 1024programmer.com

© 2023 1024programmer - Encyclopedia of Programming Field
Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us