手机
当前位置:查字典教程网 >编程开发 >C#教程 >wpf将表中数据显示到datagrid示例
wpf将表中数据显示到datagrid示例
摘要:a.在.xaml文件中拖入一个datagrid,然后添加列名,使用Binding="{Binding数据库中的列名称}",如下:复制代码代码...

a.在.xaml文件中拖入一个datagrid,然后添加列名,使用Binding="{Binding 数据库中的列名称}",如下:

复制代码 代码如下:

<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中

复制代码 代码如下:

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;

【wpf将表中数据显示到datagrid示例】相关文章:

c# 删除所有的空文件夹的小例子

C#中 paint()与Onpaint()的区别

C#保存图片到数据库并读取显示图片的方法

c# Base关键字的使用

C#数据结构揭秘一

基于Silverlight DataGrid中无代码设置开始与结束日期DatePicker的实现方法

C#中动态显示当前系统时间的实例方法

关于Flyweight模式应用实践的相关介绍

使用C#实现阿拉伯数字到大写中文的转换

c#中返回文章发表的时间差的示例

精品推荐
分类导航