zoukankan      html  css  js  c++  java
  • WPF 控件DataGrid绑定

    WPF 手动绑定 DataGrid 例子:
    前台:
    <DataGrid AutoGenerateColumns="False" Name="dataGrid1">
         <DataGrid.Columns>
            <DataGridTextColumn Header="商品名称" Binding="{Binding Path=GoodsName}"></DataGridTextColumn>
            <DataGridTextColumn Header="内容" Binding="{Binding Path=GoodsIntroduce}"></DataGridTextColumn>
         </DataGrid.Columns>
    </DataGrid>
    后台:
     private void button1_Click(object sender, RoutedEventArgs e)
            {
                string sql = "server=.;database=db_Tome1;uid=sa;pwd=123456";//连接字符串
                SqlConnection sqlcon = new SqlConnection(sql);//

                textBox1.Text = "AAAA";
                string date = datePicker1.Text;
                string sqlstr = "select GoodsName from tb_GoodsInfo";
                SqlDataAdapter da = new SqlDataAdapter(sqlstr, sqlcon);
                DataSet ds = new DataSet();
                ds.Clear();
                DataTable table1 = new DataTable();
                sqlcon.Open();
                da.Fill(ds, "table1");
                dataGrid1.ItemsSource = ds.Tables[0].DefaultView;
                sqlcon.Dispose();
                sqlcon.Close();
               
            }
    WPF 自动绑定 DataGrid 例子:加 ItemsSource
    前台:加 ItemsSource
     <DataGrid AutoGenerateColumns="True" Height="200" HorizontalAlignment="Left" Margin="37,63,0,0"

    ItemsSource="{Binding Path=table1}" Name="dataGrid1" VerticalAlignment="Top" Width="200"  ></DataGrid>
    后台:
    private void button1_Click(object sender, RoutedEventArgs e)
            {
                string sql = "server=.;database=db_Tome1;uid=sa;pwd=123456";//连接字符串
                SqlConnection sqlcon = new SqlConnection(sql);//

                textBox1.Text = "AAAA";
                string date = datePicker1.Text;
                string sqlstr = "select * from tb_GoodsInfo";
                SqlDataAdapter da = new SqlDataAdapter(sqlstr, sqlcon);
                DataSet ds = new DataSet();
                ds.Clear();
                DataTable table1 = new DataTable();
                sqlcon.Open();
                da.Fill(ds, "table1");
                dataGrid1.DataContext = DataContext = ds;
                sqlcon.Dispose();
                sqlcon.Close();
               
            }
    WPF 自动绑定 DataGrid 例子:不带ItemsSource的
    前台:
    <DataGrid AutoGenerateColumns="True" Height="200" HorizontalAlignment="Left" Margin="37,63,0,0"

    Name="dataGrid1" VerticalAlignment="Top" Width="200"  ></DataGrid>
    后台:
    dataGrid1.ItemsSource = ds.Tables[0].DefaultView;

  • 相关阅读:
    APNS 证书生成注意事项
    Remove a Submodule within git
    Quartz.NET
    Linux.net && mono
    系统整理qt笔记1
    串口通讯之rs232 c++版本
    串口通信 之 linux固定串口别名的两种方法
    系统整理qt笔记3
    串口通信+进制基础+位运算
    qt(二)
  • 原文地址:https://www.cnblogs.com/zhaoliang831214/p/3532224.html
Copyright © 2011-2022 走看看