ItemsControl 类绑定数据库
2015-09-30 22:52
274 查看
// .cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; 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; using System.Data.SqlClient; using System.Configuration; using System.Collections.ObjectModel; using System.ComponentModel; namespace ListView { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { // 定义连接字符串 static public string connString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; // 创建 Connection 对象 static public SqlConnection conn = new SqlConnection(connString); public MainWindow() { InitializeComponent(); string sql = String.Format("select * from t_xtgl_czry"); conn.Open(); SqlCommand comm = new SqlCommand(sql, conn); SqlDataReader dr = comm.ExecuteReader(); while (dr.Read()) { listCustomer.Add(new UserInfo(dr["rybm"].ToString(), dr["name"].ToString(), dr["qx"].ToString())); } lv.ItemsSource = listCustomer; dr.Close(); conn.Close(); // 设置默认选中第一项 lv.SelectedIndex = 0; } public class UserInfo : INotifyPropertyChanged { #region INotifyPropertyChanged 成员 public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(PropertyChangedEventArgs e) { if (PropertyChanged != null) { PropertyChanged(this, e); } } #endregion private string _employeeNumber; private string _Name; private string _Competence; public string EmployeeNumber { get { return _employeeNumber; } set { _employeeNumber = value; OnPropertyChanged(new PropertyChangedEventArgs("EmployeeNumber")); } } public string Name { get { return _Name; } set { _Name = value; OnPropertyChanged(new PropertyChangedEventArgs("Name")); } } public string Competence { get { return _Competence; } set { _Competence = value; OnPropertyChanged(new PropertyChangedEventArgs("Competence")); } } public UserInfo(string empnumber, string name, string Competence) { _employeeNumber = empnumber; _Name = name; _Competence = Competence; } } ObservableCollection<UserInfo> listCustomer = new ObservableCollection<UserInfo>(); } }
// .xaml <Window x:Class="ListView.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:c="clr-namespace:ListView" Title="ListViewSample" Height="305" Width="372"> <Grid> <ListView Name="lv" HorizontalAlignment="Left" Height="155" Margin="34,88,0,0" VerticalAlignment="Top" Width="300"> <ListView.View> <GridView> <GridViewColumn DisplayMemberBinding="{Binding Path=EmployeeNumber}" Header="人员编码" Width="100"/> <GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Header="姓名" Width="100"/> <GridViewColumn DisplayMemberBinding="{Binding Path=Competence}" Header="权限" Width="100"/> </GridView> </ListView.View> </ListView> <Label Content="FullName:" HorizontalAlignment="Left" Margin="34,49,0,0" VerticalAlignment="Top" Width="71" Height="25"/> <TextBox HorizontalAlignment="Left" Height="25" Margin="119,49,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="211" VerticalContentAlignment="Center"/> </Grid> </Window>
相关文章推荐
- ItemsControl 类绑定数据库
- Mysql数据库锁定机制
- 第三次作业---excel导入数据库及显示
- mysql以及oracle的递归查询
- mysql权限存取过程
- mysql主从同步
- 一:redis 的string类型 - 相关操作
- Mysql 列转行统计查询 、行转列统计查询
- jdbc数据库的连接
- 开放3306端口,远程连接MySQL
- 【错误处理】mysql远程连接ERROR 2003 (HY000): Can't connect to MySQL server on '10.211.55.6' (113)
- Nosql数据库——redis(六)事务和持久化
- mysql 打开远程服务
- [mysql实战] mysql基本操作 下
- redis python-redis 安装详细步骤
- redis 队列操作的例子(php)
- Ehcache与Memcached比较
- SQL Server ->> 重命名数据库
- redis 操作大全 PHP-redis中文文档
- iOS软件开发 数据存储之SQLite3