VB或C#的WPF中DataGrid控件如何绑定DataTable表

2024-10-28 00:04:51
推荐回答(3个)
回答1:

直接绑定到数据库里的table的
后台代码:
MKP.MyCommon.SQLHelper sh = new MKP.MyCommon.SQLHelper(null);
DataTable dt=sh.GetDataTable("select Fi_id,Fs_pCompanyName,Fs_phone,Fs_email from TpmCompany");
dataGrid1.ItemsSource = dt.DefaultView;  
前台代码:

Height="153" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top" 
Width="449" SelectedCellsChanged="dataGrid1_SelectedCellsChanged">






回答2:

dataGrid1.ItemsSource = dt.AsDataView();

dataGrid的AutoGenerateColumns属性必须为true

回答3:

直接绑定到数据库里的table的
后台代码:
MKP.MyCommon.SQLHelper
sh
=
new
MKP.MyCommon.SQLHelper(null);
DataTable
dt=sh.GetDataTable("select
Fi_id,Fs_pCompanyName,Fs_phone,Fs_email
from
TpmCompany");
dataGrid1.ItemsSource
=
dt.DefaultView;  
前台代码:
class="csharp"
name="code">AutoGenerateColumns="False"
Height="153"
HorizontalAlignment="Left"
Name="dataGrid1"
VerticalAlignment="Top"
Width="449"
SelectedCellsChanged="dataGrid1_SelectedCellsChanged">
>
Header="ID"
Binding="{Binding
Path=Fi_id}"/>
Header="公司名称"
Binding="{Binding
Path=Fs_pCompanyName}"/>
Binding="{Binding
Path=Fs_phone}"
Header="电话"
/>
Binding="{Binding
Path=Fs_email}"
Header="Email">