1. Add the DataGrid on the Window.
2. Click the columns property button[...] on the Properties of DataGrid
3. Add the three colums to the DataGrid.
4. Type the Binding attribute on the DataGridTextColumn tag.
1 2 3 4 5 6 7 | <DataGrid AutoGenerateColumns="False" Height="217" HorizontalAlignment="Left" Margin="38,56,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="420"> <DataGrid.Columns> <DataGridTextColumn Header="ID" Binding="{Binding Path=ID}"/> <DataGridTextColumn Header="Name" Binding="{Binding Path=NAME}"/> <DataGridTextColumn Header="Telephone" Binding="{Binding Path=TEL_NO}"/> </DataGrid.Columns> </DataGrid> |
5. Add the Loaded event of the Window.
6. Type the below code to the Window's Loaded event.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Data; namespace DataGridBinding { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { // Create DataTable instance. DataTable dataTable = new DataTable(); // Add the columns. dataTable.Columns.Add("ID", typeof(string)); dataTable.Columns.Add("NAME", typeof(string)); dataTable.Columns.Add("TEL_NO", typeof(string)); // Add the datas. dataTable.Rows.Add(new string[] { "ID-01", "Name 01", "010-0001-0000" }); dataTable.Rows.Add(new string[] { "ID-02", "Name 02", "010-0002-0000" }); dataTable.Rows.Add(new string[] { "ID-03", "Name 03", "010-0003-0000" }); dataTable.Rows.Add(new string[] { "ID-04", "Name 04", "010-0004-0000" }); // Binding DataView of DataTable. dataGrid1.ItemsSource = dataTable.DefaultView; } } } |
※ Refer below binding the columns of DataTable and DataGrid
7. Below image is result.
Refer below video.
No comments:
Post a Comment