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;
相关文章
- 11-29WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决
- 11-29整理:WPF中应用附加事件制作可以绑定命令的其他事件
- 11-29WPF中DataGrid垂直滚动条滚动后导致每行CheckBox选择错乱
- 11-29WPF MVVM 用户控件完成分页
- 11-29WPF嵌入Winform控件置顶问题
- 11-29WPF绑定文本时使用指定格式文本
- 11-29WPF多个列表视图绑定到具有不同排序说明的同一集合
- 11-29flex中DataGrid里使用itemRenderer后数据无法绑定到数据源的问题
- 11-29WPF自定义数字输入框控件
- 11-29easyui datagrid动态修改editor时动态绑定combobox的数据