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;

WPF 控件DataGrid绑定

上一篇:Safari 中加载 Flash 使用overflow失效的bug


下一篇:博客样式