wpf將表中數(shù)據(jù)顯示到datagrid示例
a.在.xaml文件中拖入一個datagrid,然后添加列名,使用Binding="{Binding 數(shù)據(jù)庫中的列名稱}",如下:
<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.首先把要顯示的數(shù)據(jù)查詢后放入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中的數(shù)據(jù)與datagrid綁定
dataGrid1.ItemsSource = Show().DefaultView;
相關文章
C# 微信支付 wx.chooseWXPay 簽名錯誤的解決方法
本篇文章主要介紹了C# 微信支付 wx.chooseWXPay 簽名錯誤的解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12C#結合SMTP實現(xiàn)郵件報警通知的實現(xiàn)示例
這篇文章主要介紹了C#結合SMTP實現(xiàn)郵件報警通知的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07C#設置WinForm中DataGrid列的方法(列寬/列標題等)
這篇文章主要介紹了C#設置WinForm中DataGrid列的方法,包括列寬、列標題等部分,并分析了其中相關的操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07