源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

wpf将表中数据显示到datagrid示例

  • 时间:2020-12-09 06:21 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:wpf将表中数据显示到datagrid示例
a.在.xaml文件中拖入一个datagrid,然后添加列名,使用Binding="{Binding 数据库中的列名称}",如下: 
[u]复制代码[/u] 代码如下:
<DataGrid AutoGenerateColumns="False" Height="438"HorizontalAlignment="Left" Margin="23,278,0,0" Name="dataGrid1"  VerticalAlignment="Top" Width="1249">                <DataGrid.Columns>                    <DataGridTextColumn Width="100" FontSize="15" Header="编号" Binding="{Binding id}"/>                               <DataGridTextColumn Width="140" Header="名称" FontSize="15"  Binding="{Binding name}"/>                     </DataGrid.Columns>        </DataGrid>
b.首先把要显示的数据查询后放入datatable中  
[u]复制代码[/u] 代码如下:
public DataTable Show() {            DataTable dt = new DataTable();            try            { if (DBHelper.connection.State == ConnectionState.Closed)                    DBHelper.connection.Open();                string sql = "查询语句";                DataSet ds = new DataSet();                SqlDataAdapter sda = new SqlDataAdapter(sql,DBHelper.connection);                sda.Fill(ds, "虚拟表名");                dt= ds.Tables["虚拟表名"];            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }            return dt;        } //注意:该方法中的虚拟表名就是一个自己定义的表名称 c.然后在后台代码编辑处将datatable中的数据与datagrid绑定  dataGrid1.ItemsSource = Show().DefaultView;
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部