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入门(4)

    一.while循环 有时候,你可能需要计算机来帮重复做一件事,这时就需要循环. while condition: statements (else: statements ) 当condition条件 ...

  2. java一些面试题

    java虚拟机 什么时候会触发full gc System.gc()方法的调用 老年代空间不足 永生区空间不足(JVM规范中运行时数据区域中的方法区,在HotSpot虚拟机中又被习惯称为永生代或者永生 ...

  3. NFC学习总结二

    移动支付这事情热了总归还是会回归理性,就如同之前的10几年间的几次轮回一样.字面上看,移动支付比支付大也不大可能,有相同,有扩展,有交集有不通才是. NFC这事情也是说了快十年了,真心希望它能回归到其 ...

  4. eg_4

    4. 编写一个程序,要求以树状结构展现特定的文件夹及其子文件(夹) import java.io.*; public class Test { public static void main(Stri ...

  5. 【week6】用户数

    小组名称:nice! 小组成员:李权 于淼 杨柳 刘芳芳 项目内容:约跑app alpha发布48小时以后用户数如何,是否达到预期目标,为什么,是否需要改进,如何改进(或理性估算). 首先我们的app ...

  6. vue服务端渲染简单入门实例

    想到要学习vue-ssr的同学,自不必多说,一定是熟悉了vue,并且多多少少做过几个项目.然后学习vue服务端渲染无非解决首屏渲染的白屏问题以及SEO友好. 话不多说,笔者也是研究多日才搞明白这个服务 ...

  7. 大全Kafka Streams

    本文将从以下三个方面全面介绍Kafka Streams 一. Kafka Streams 概念 二. Kafka Streams 使用 三. Kafka Streams WordCount   一. ...

  8. nginx 设置默认虚拟 host

    nginx 设置默认虚拟 host listren 80 default_server

  9. ErrorUnable to tunnel through proxy. Proxy returns HTTP1.1 400 Bad Reques

    导入项目的时候,一般会出现这种错误,因为我们的gradle版本,不对,所以默认AS导入后,回去下载你需要的gradle,所以很慢, 先打开:项目路径底下的\gradle\wrapper\gradle- ...

  10. 前端基础:JavaScript介绍

    JavaScript介绍 一.JavaScript简介 1.在1995年时,由Netscape公司的Brendan Eich,在网景导航者浏览器上首次设计实现二层,因为Netscape与Sun合作,N ...