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;
相關(guān)文章
c# OpenCvSharp實現(xiàn)常見檢測(斑點檢測,輪廓檢測,邊緣檢測)
這篇文章主要為大家詳細介紹了c#如何使用OpenCvSharp實現(xiàn)常見檢測(斑點檢測,輪廓檢測,邊緣檢測),文中的示例代碼講解詳細,需要的小伙伴可以參考下2023-12-12
解析美國東部時間與北京時間相互轉(zhuǎn)換的實現(xiàn)代碼
本篇文章是對美國東部時間與北京時間相互轉(zhuǎn)換的實現(xiàn)代碼進行了詳細的分析介紹,需要的朋友參考下2013-05-05

