wpf将表中数据显示到datagrid示例
时间:2023-06-13 04:14:06
a.在.xaml文件中拖入一个datagrid,然后添加列名,使用Binding="{Binding 数据库中的列名称}",如下:
<DataGrid AutoGenerateColumns="False" Height="438"HorizontalAlignment="Left" Margin="23,278,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="1249">
<DataGrid.Columns>
<DataGridTextColumn Width="100" FontSize="15" Header="编号" Binding="{Binding id}"/>
<DataGridTextColumn Width="140" Header="名称" FontSize="15" Binding="{Binding name}"/>
</DataGrid.Columns>
</DataGrid>
b.首先把要显示的数据查询后放入datatable中
public DataTable Show()
{
DataTable dt = new DataTable();
try
{
if (DBHelper.connection.State == ConnectionState.Closed)
DBHelper.connection.Open();
string sql = "查询语句";
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(sql,DBHelper.connection);
sda.Fill(ds, "虚拟表名");
dt= ds.Tables["虚拟表名"];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return dt;
}
//注意:该方法中的虚拟表名就是一个自己定义的表名称
c.然后在后台代码编辑处将datatable中的数据与datagrid绑定
dataGrid1.ItemsSource = Show().DefaultView;
标签:wpf,datagrid
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java C++题解leetcode904水果成篮
2023-12-08 21:06:09
![](https://img.aspxhome.com/file/2023/3/89953_0s.png)
Android界面 NotificationManager使用Bitmap做图标
2023-06-13 11:59:42
Java实战之在线寄查快递系统的实现
2023-08-10 21:52:23
![](https://img.aspxhome.com/file/2023/2/58182_0s.png)
C#(asp.net)多线程用法示例(可用于同时处理多个任务)
2022-03-09 01:41:30
c#使用正则表达式匹配字符串验证URL示例
2023-01-01 10:40:10
idea中Maven镜像源详细配置步骤记录(对所有项目)
2022-12-17 14:27:56
![](https://img.aspxhome.com/file/2023/8/88198_0s.png)
java基础之String知识总结
2022-09-24 16:25:20
![](https://img.aspxhome.com/file/2023/9/118839_0s.png)
浅谈Spring事务传播行为实战
2022-07-04 01:54:44
![](https://img.aspxhome.com/file/2023/0/86860_0s.jpg)
Java 使用 FFmpeg 处理视频文件示例代码详解
2023-03-19 00:54:37
monkeyrunner之电脑安装驱动(5)
2022-02-28 19:17:00
![](https://img.aspxhome.com/file/2023/4/87334_0s.png)
java web项目里ehcache.xml介绍
2022-02-25 20:46:25
IDEA2022创建Maven Web项目教程(图文)
2022-09-25 02:30:44
![](https://img.aspxhome.com/file/2023/9/108179_0s.jpg)
Java深入了解数据结构中常见的排序算法
2021-11-02 23:33:53
![](https://img.aspxhome.com/file/2023/8/68138_0s.png)
java实现创建临时文件然后在程序退出时自动删除文件
2023-02-15 18:27:00
![](https://img.aspxhome.com/file/2023/0/105920_0s.png)
浅谈C#与Java两种语言的比较
2023-09-26 13:05:19
Java实现双保险线程的示例代码
2023-09-21 07:59:46
Java 多线程并发LockSupport
2022-04-07 23:59:57
![](https://img.aspxhome.com/file/2023/5/91535_0s.png)
C# Aspose.Words 删除word中的图片操作
2023-07-29 12:01:43
![](https://img.aspxhome.com/file/2023/5/96325_0s.jpg)
Android 动画之RotateAnimation应用详解
2022-08-17 17:03:07
![](https://img.aspxhome.com/file/2023/0/137150_0s.png)
详解C#中的依赖注入和IoC容器
2023-03-11 09:05:19
![](https://img.aspxhome.com/file/2023/2/82232_0s.png)