我将数据动态绑定到数据网格,但它不显示数据。它给了我这五列:RowError、RowState、table、ItemArray 和 HasError。
但是行数是正确的,它给出的行数与我在数据库中的行数相同。
这是VB代码:
Dim con As New OdbcConnection("dsn=PAUSPAN")
con.Open()
Dim cmd As New OdbcCommand("select * from tbl_chart", con)
Dim da As New OdbcDataAdapter(cmd)
'Dim dt As New DataTable("a")
Dim ds As New DataSet()
ds.Tables.Add("a")
da.Fill(ds, "a")
MsgBox(ds.Tables("a").Rows.Count.ToString)
DataGrid1.ItemsSource = ds.Tables("a").AsEnumerable.ToList()
'DataGrid1.DataContext = ds.DefaultViewManager
con.Close()
这是 XAML 代码:
<Window x:Class="datagrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="datagrid" Height="344" Width="599">
<Grid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="486,225,0,0" Name="Button1"
VerticalAlignment="Top" Width="75" />
<DataGrid Height="241" HorizontalAlignment="Left"
Margin="12,12,0,0"
Name="DataGrid1" VerticalAlignment="Top"
Width="386" ItemsSource="{Binding ds}" />
</Grid>
</Window>
如何解决这个问题呢?它如何动态地将数据集绑定到数据网格?