当前位置:网站首页>WPF DataGrid 展示数据
WPF DataGrid 展示数据
2022-08-08 18:35:00 【flysh05】
DataGrid 经常用作数据的展示,例如:

1. UI设计
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<!--Row0-->
<TextBlock Grid.Row="0" HorizontalAlignment="Center" Foreground="Green" FontStyle="Italic" FontSize="25">
Using DataGrid Control Show Data
</TextBlock>
<DataGrid Grid.Row="1" Margin="5" AutoGenerateColumns="False" x:Name="simpleDemoDataGrid" HorizontalGridLinesBrush="LightGreen" CanUserAddRows="False" IsReadOnly="True" VerticalGridLinesBrush="Black">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID}" Width="50"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="100"/>
<DataGridTextColumn Header="PhoneNumber" Binding="{Binding PhoneNumber}" Width="120"/>
<DataGridTextColumn Header="Address" Binding="{Binding Address}" Width="200"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
2. 后台代码
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.simpleDemoDataGrid.ItemsSource = CreteDemoData().DefaultView;
this.simpleDemoDataGrid.GridLinesVisibility = DataGridGridLinesVisibility.All;
}
static DataTable CreteDemoData()
{
DataTable dt = new System.Data.DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("PhoneNumber", typeof(string));
dt.Columns.Add("Address", typeof(string));
DataRow row = dt.NewRow();
row["ID"] = 1;
row["Name"] = "Jack";
row["PhoneNumber"] = "123456";
row["Address"] = "Beijin";
dt.Rows.Add(row);
row = dt.NewRow();
row["ID"] = 2;
row["Name"] = "Tom";
row["PhoneNumber"] = "789001";
row["Address"] = "Shanghai";
dt.Rows.Add(row);
row = dt.NewRow();
row["ID"] = 3;
row["Name"] = "Jerry";
row["PhoneNumber"] = "67767";
row["Address"] = "Shenzhen";
dt.Rows.Add(row);
return dt;
}
}
创建一个DataTable 数据源,在窗口初始化时绑定。DataTable的原始数据可以来自数据库,文件等。
边栏推荐
- 响应式pbootcms模板电脑维修类网站
- 21天学习挑战赛——机器学习01
- openEuler 社区 2022 年 7 月运作报告
- Why do programmers only close monitor from none computer after work?Look at the answer ~ each big web site
- 为啥程序员下班后只关显示器从不关电脑?看看各大网站的答案~
- 传音控股:目前公司手机产品暂无明确计划进入中国市场
- feign的性能优化、Feign的使用-最佳优化两种方案
- Learn about layered architecture & SOA architecture together
- 达梦数据库 DmAPservice服务,启停影响 DMSERVER库服务吗?
- 数据库学习之表的操作
猜你喜欢

2021年9月电子学会图形化一级编程题解析含答案:小狗进圈

Shell脚本三剑客(grep、sed、awk)

MogDB study notes - starting from 0

2022年6月电子学会考级试卷真题解析(含答案和所有文档下载)

How to add F4 Value Help to the input parameters of the report in the ABAP report

Laravel 队列消费实例和定时任务添加任务消费

LabVIEW报错“仪器IO助手未正确安装”

16. Learn Lua file I/O together

Build DG will increase the amount of lead to archive log problem

Fortinet new cloud native protection products launched amazon cloud platform of science and technology
随机推荐
flask基础知识:
21天学习挑战赛——机器学习02
uniapp parent component uses prop to pass asynchronous data to child components
阿里云数据库PolarDB开源人才培养计划发布!万元好礼等你来拿!
Oracle--表
常用字符的编码
【kali-权限提升】(4.2.6)社会工程学工具包(上):中间人攻击原理
Shell正则表达式
Flush can buy stock?Is it safe to buy stocks?
Open Office XML 格式中的 Style 设计原理
Is there any function in MAXCOMPUTE SQL to judge whether the string is a number?
请问shake数据库中mongoshake同步过程中,src_mongo挂了,同步服务不会退出吗?
做测试几年,靠业务熟悉吃老本,技术短板暴露,30岁被无情辞退...
SSH协议抓包-工具Wireshark
CAD进阶练习(二)
USB CY68013设备描述符识别失败
Redis Server启动过程
2021年9月电子学会图形化一级编程题解析含答案:小狗进圈
【761. Special binary sequence】
卡通渲染的历史