WPF DataGrid、ListView 简单绑定
DataGrid运行效果: xaml 代码: DataGridName= dtgData ItemsSource= {Binding} AutoGenerateColumns= False DataGrid.Columns DataGridTextColumnBinding= {BindingPath=id} Header= ID HeaderStringFormat= id / DataGridTextColumnBinding= {BindingPath=name} Header= 名称 Header
DataGrid运行效果:

xaml 代码:
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=id}" Header="ID" HeaderStringFormat="id"/>
<DataGridTextColumn Binding="{Binding Path=name }" Header="名称" HeaderStringFormat="name" />
<DataGridTextColumn Binding="{Binding Path=level }" Header="等级" HeaderStringFormat="level" />
</DataGrid.Columns>
</DataGrid>
cs 代码:
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=192.168.....;database=hospital;uid=sa;pwd=123456";
SqlCommand comm = new SqlCommand();
string strSql="select * from address";
comm.CommandType = CommandType.Text;
DataTable dt = new DataTable();
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
sda.Fill(dt);
dtgData.ItemsSource = dt.DefaultView;
}
ListView 运行效果:

xaml 代码:
<ListView.View >
<GridView x:Name="gvw1">
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding Path=id}"></GridViewColumn>
<GridViewColumn Header="名称" DisplayMemberBinding="{Binding Path=name}"></GridViewColumn>
<GridViewColumn Header="等级" DisplayMemberBinding="{Binding Path=level}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
cs 代码:
private void BindData(string strSql)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=.;database=hospital;uid=sa;pwd=123456";
SqlCommand comm = new SqlCommand();
comm.CommandType = CommandType.Text;
DataTable dt = new DataTable();
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
sda.Fill(dt);
ltvData.ItemsSource = dt.DefaultView;
}
本文来自zhangqynuli的博客,原文地址:http://hi.baidu.com/zhangqynuli/blog/item/f96d41c9236a6359f31fe707.html
WPF DataGrid、ListView 简单绑定的更多相关文章
- WPF DataGrid ListView 等等 改变 选中行 颜色;以及 不变的原因
WPF中改变选中行的颜色是很简单的,就是用触发器:比如:以DataGrid为例: DataGrid.RowStyle Style TargetType= DataGridRow SetterPrope ...
- 【WPF】WPF DataGrid List数据源 双向绑定通知机制之ObservableCollection使用以及MultiBinding 的应用
以下代码实现了DataGrid的简单绑定List数据源 重点要提一下的是,绑定List数据源,但是不能直接用List.比如下面的代码,使用List<GridItem>只能实现数据修改的绑定 ...
- WPF 实现 DataGrid/ListView 分页控件
在WPF中,通常会选用DataGrid/ListView进行数据展示,如果数据量不多,可以直接一个页面显示出来.如果数据量很大,2000条数据,一次性显示在一个页面中,不仅消耗资源,而且用户体验也很糟 ...
- WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.
WPF DataGrid某列使用多绑定后该列排序失效 2011-07-14 10:59hdongq | 浏览 1031 次 悬赏:20 在wpf的datagrid中某一列使用了多绑定,但是该列排序失 ...
- WPF DataGrid 绑定DataSet数据 自动生成行号
1.绑定数据:dataGrid1.ItemsSource = dataSet.Tables[0].DefaultView; 注意:在创建DataGrid 时可以通过AutoGenerateColumn ...
- WPF DataGrid绑定一个组合列
WPF DataGrid绑定一个组合列 前台: <Page.Resources> <local:InfoConverter x:Key="converter& ...
- WPF DataGrid 绑定行双击行命令
WPF DataGrid 绑定行双击行命令 <DataGrid ...> <DataGrid.InputBindings> <MouseBinding MouseActi ...
- WPF dataGrid下的ComboBox的绑定
WPF dataGrid下的ComboBox的绑定 Wpf中dataGrid中的某列是comboBox解决这个问题费了不少时间,不废话了直接上代码 xaml 代码 <DataGridTempla ...
- WPF DataGrid 双击行 获得绑定数据
原文:WPF DataGrid 双击行 获得绑定数据 1)增加事件 2)增加对象获取 1)事件代码 Datagrid 增加事件 MouseDoubleClick="dataGrid_Mous ...
随机推荐
- Ext JS 6学习文档-第4章-数据包
Ext JS 6学习文档-第4章-数据包 数据包 本章探索 Ext JS 中处理数据可用的工具以及服务器和客户端之间的通信.在本章结束时将写一个调用 RESTful 服务的例子.下面是本章的内容: 模 ...
- JavaScript筑基篇(一)->变量、值与对象
说明 JavaScript中变量.值.对象的理解.本文为了简化理解,前半部分暂时刨除与执行上下文的相关概念.另外本文是个人的见解,如有疑问或不正支持,欢迎提出指正和讨论! 目录 前言 参考来源 变量与 ...
- StrBlobPtr类——weak_ptr访问vector元素
#include <iostream> #include <memory> #include <string> #include <initializer_l ...
- 路由器如何设置上网(TP-LINK)
最近宿舍公用的网络一直不太稳定,正赶上毕业季,本来就打算自己买一台自用的路由器,于是我从一个毕业的师姐手里15RMB收了一台路由器,师姐还给了我一根5m的网线和两根全新15m的,感觉光网线就赚翻了. ...
- 软件工程 作业part1 自我介绍
自我介绍 老师您好,我叫宋雨,本科在长春理工大学,专业是计算机科学与技术. 1.回想一下你曾经对计算机专业的畅想:当初你是如何做出选择计算机专业的决定?你认为过去接触的课程是否符合你对计算机专业的期待 ...
- 20145214 《Java程序设计》第5周学习总结
20145214 <Java程序设计>第5周学习总结 教材学习内容总结 try和catch Java中所有错误都会被包装为对象,可以尝试try执行程序并捕捉catch代表错误的对象后做一些 ...
- DNS域名解析协议
一. 根域 就是所谓的“.”,其实我们的网址www.baidu.com在配置当中应该是www.baidu.com.(最后有一点),一般我们在浏览器里输入时会省略后面的点,而这也已经成为了习惯. 根域服 ...
- 3dContactPointAnnotationTool开发日志(七)
调了半天发现是逻辑错误,改了一下终于没那么奇怪了: 但是有的接触点很明显跑偏了.再回顾一下自己是怎么求的,我是直接用的下面的代码求解一个点是否在另一个物体内部: var bounds = us ...
- (转)《linux性能及调优指南》 3.3 内存瓶颈
翻译:Hank (http://blog.csdn.net/fireroll)版权所有,尊重他人劳动成果,转载时请注明作者和原始出处及本声明.原文名称:<Linux Performance an ...
- opencv里vector的内存的申请和释放的问题
改成: ); CvSeq * m_contour=; cvFindContours( &IPlImage(img), m_storage, &m_contour, ,)); //释放内 ...