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 Name="dtgData"  ItemsSource="{Binding}"  AutoGenerateColumns="False">
            <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 代码:

       private void BindData()
        {
            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  Name="ltvData">
            <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 简单绑定的更多相关文章

  1. WPF DataGrid ListView 等等 改变 选中行 颜色;以及 不变的原因

    WPF中改变选中行的颜色是很简单的,就是用触发器:比如:以DataGrid为例: DataGrid.RowStyle Style TargetType= DataGridRow SetterPrope ...

  2. 【WPF】WPF DataGrid List数据源 双向绑定通知机制之ObservableCollection使用以及MultiBinding 的应用

    以下代码实现了DataGrid的简单绑定List数据源 重点要提一下的是,绑定List数据源,但是不能直接用List.比如下面的代码,使用List<GridItem>只能实现数据修改的绑定 ...

  3. WPF 实现 DataGrid/ListView 分页控件

    在WPF中,通常会选用DataGrid/ListView进行数据展示,如果数据量不多,可以直接一个页面显示出来.如果数据量很大,2000条数据,一次性显示在一个页面中,不仅消耗资源,而且用户体验也很糟 ...

  4. WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.

    WPF DataGrid某列使用多绑定后该列排序失效 2011-07-14 10:59hdongq | 浏览 1031 次  悬赏:20 在wpf的datagrid中某一列使用了多绑定,但是该列排序失 ...

  5. WPF DataGrid 绑定DataSet数据 自动生成行号

    1.绑定数据:dataGrid1.ItemsSource = dataSet.Tables[0].DefaultView; 注意:在创建DataGrid 时可以通过AutoGenerateColumn ...

  6. WPF DataGrid绑定一个组合列

    WPF DataGrid绑定一个组合列 前台: <Page.Resources>        <local:InfoConverter x:Key="converter& ...

  7. WPF DataGrid 绑定行双击行命令

    WPF DataGrid 绑定行双击行命令 <DataGrid ...> <DataGrid.InputBindings> <MouseBinding MouseActi ...

  8. WPF dataGrid下的ComboBox的绑定

    WPF dataGrid下的ComboBox的绑定 Wpf中dataGrid中的某列是comboBox解决这个问题费了不少时间,不废话了直接上代码 xaml 代码 <DataGridTempla ...

  9. WPF DataGrid 双击行 获得绑定数据

    原文:WPF DataGrid 双击行 获得绑定数据 1)增加事件 2)增加对象获取 1)事件代码 Datagrid 增加事件 MouseDoubleClick="dataGrid_Mous ...

随机推荐

  1. python常用命令—终端安装win32的两种方法

    1, pip install pywin32 2, pip install pypiwin32

  2. openstack对接VMware浅析

    前言 本文是对openstack对接vmware的浅析,所以本文重点是以下两点: 先了解它的整体架构,搞清楚为什么要用这样的架构: 然后再了解架构中的各个组件,组件提供的主要功能与各个组件之间的交互 ...

  3. HADOOP docker(九):hdfs权限

    1. 概述2. 用户身份标识3. 组映射4.关于权限的实现5.文件系统API的变更6.应用程序shell的变更7.超级用户8.ACLs9.ACL 文件系统API10.ACL命令11.参数配置12.总结 ...

  4. js如何处理字符串中带有↵字符

    js或vue中如何处理字符串中带有↵字符 split('\n') 使用split('\n')将字符串分割成数组就行 如果我们在vue中,只需要在页面中绑定变量时操作split('\n')就可以了: & ...

  5. HTML5 canvas制作童年的回忆大风车

    今天看到一篇CSS3写的大风车http://www.cnblogs.com/yaojaa/archive/2013/01/30/2882521.html,感觉CSS3太神奇了,这在以前用CSS是想都不 ...

  6. 《剑指Offer》题六十一~题六十八

    六十一.扑克牌中的顺子 题目:从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的.2~10为数字本身,A为1,J为11,Q为12,K为13,而大.小王可以看成任意数字. 六十二.圆圈中 ...

  7. The New Day

    于博毅 160809107 爱好电脑研究 选大学专业的时候,把计算机类放在了第一专业,当时从小就很喜欢计算机,以前有接触过编程但仅限于看书,并没有动手实践过,选课的时候看了一下专业课程,都是我想学的 ...

  8. MATLAN中矩阵表示中有一维是~表示的意思

    [~,m]表示的意思解释. [~,m]=rat(12/34) m = 17 >> [x,m]=rat(12/34) x = 6 m = 17

  9. BZOJ 1406 密码箱(数论)

    很简洁的题目.求出x^2%n=1的所有x<=n的值. n<=2e9. 直接枚举x一定是超时的. 看看能不能化成有性质的式子. 有 (x+1)(x-1)%n==0,设n=a*b,那么一定有x ...

  10. 【bzoj1858】[Scoi2010]序列操作 线段树区间合并

    题目描述 lxhgww最近收到了一个01序列,序列里面包含了n个数,这些数要么是0,要么是1,现在对于这个序列有五种变换操作和询问操作: 0 a b 把[a, b]区间内的所有数全变成0 1 a b ...