VB或C#的WPF中DataGrid控件如何绑定DataTable表
在WinForm中我们可以很简单的DimdtAsNewSystem.Data.DataTableDataGridView1.DataSource=dt这样就可以在Data...
在WinForm中我们可以很简单的
Dim dt As New System.Data.DataTable
DataGridView1.DataSource = dt
这样就可以在DataGridView中绑定DataTable了
但是在WPF的DataGrid中没有DataSource属性
搞了半天没弄懂怎么绑定DataTable
望大虾们指教 展开
Dim dt As New System.Data.DataTable
DataGridView1.DataSource = dt
这样就可以在DataGridView中绑定DataTable了
但是在WPF的DataGrid中没有DataSource属性
搞了半天没弄懂怎么绑定DataTable
望大虾们指教 展开
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;
前台代码:
<pre class="csharp" name="code"><DataGrid AutoGenerateColumns="False"
Height="153" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top"
Width="449" SelectedCellsChanged="dataGrid1_SelectedCellsChanged">
<DataGrid.Columns >
<DataGridTextColumn Header="ID" Binding="{Binding Path=Fi_id}"/>
<DataGridTextColumn Header="公司名称" Binding="{Binding Path=Fs_pCompanyName}"/>
<DataGridTextColumn Binding="{Binding Path=Fs_phone}" Header="电话" />
<DataGridTextColumn Binding="{Binding Path=Fs_email}" Header="Email"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
后台代码:
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;
前台代码:
<pre class="csharp" name="code"><DataGrid AutoGenerateColumns="False"
Height="153" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top"
Width="449" SelectedCellsChanged="dataGrid1_SelectedCellsChanged">
<DataGrid.Columns >
<DataGridTextColumn Header="ID" Binding="{Binding Path=Fi_id}"/>
<DataGridTextColumn Header="公司名称" Binding="{Binding Path=Fs_pCompanyName}"/>
<DataGridTextColumn Binding="{Binding Path=Fs_phone}" Header="电话" />
<DataGridTextColumn Binding="{Binding Path=Fs_email}" Header="Email"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
展开全部
dataGrid1.ItemsSource = dt.AsDataView();
dataGrid的AutoGenerateColumns属性必须为true
dataGrid的AutoGenerateColumns属性必须为true
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
直接绑定到数据库里的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;
前台代码:
<pre
class="csharp"
name="code"><DataGrid
AutoGenerateColumns="False"
Height="153"
HorizontalAlignment="Left"
Name="dataGrid1"
VerticalAlignment="Top"
Width="449"
SelectedCellsChanged="dataGrid1_SelectedCellsChanged">
<DataGrid.Columns
>
<DataGridTextColumn
Header="ID"
Binding="{Binding
Path=Fi_id}"/>
<DataGridTextColumn
Header="公司名称"
Binding="{Binding
Path=Fs_pCompanyName}"/>
<DataGridTextColumn
Binding="{Binding
Path=Fs_phone}"
Header="电话"
/>
<DataGridTextColumn
Binding="{Binding
Path=Fs_email}"
Header="Email"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
后台代码:
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;
前台代码:
<pre
class="csharp"
name="code"><DataGrid
AutoGenerateColumns="False"
Height="153"
HorizontalAlignment="Left"
Name="dataGrid1"
VerticalAlignment="Top"
Width="449"
SelectedCellsChanged="dataGrid1_SelectedCellsChanged">
<DataGrid.Columns
>
<DataGridTextColumn
Header="ID"
Binding="{Binding
Path=Fi_id}"/>
<DataGridTextColumn
Header="公司名称"
Binding="{Binding
Path=Fs_pCompanyName}"/>
<DataGridTextColumn
Binding="{Binding
Path=Fs_phone}"
Header="电话"
/>
<DataGridTextColumn
Binding="{Binding
Path=Fs_email}"
Header="Email"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询