Reputation: 13
I'm binding data to a datagrid dynamically but it does not shows the data. It gives me this five columns in it: RowError, RowState, table, ItemArray and HasError.
But the row count is correct it gives same row count as I have in database.
This is the Vb code:
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()
And this is the XAML code:
<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>
how to solve this problem? how do it bind the dataset to datagrid dynamically?
Upvotes: 0
Views: 6883
Reputation: 81253
Bind ItemsSource
with DataView
instead of DataTable
. You can get DataView with either of the approaches mentioned below -
DataGrid1.ItemsSource = ds.Tables("a").DefaultView;
OR use extension method AsDataView
-
DataGrid1.ItemsSource = ds.Tables("a").AsDataView();
Upvotes: 2
Reputation: 3318
You are a bit mixing dynamic and static binding. you should remove ItemsSource="{Binding ds}"
from XAML and DataGrid1.DataContext = ds.DefaultViewManager
from code behind as you're communicating with ItemsSource from code behind.
Upvotes: 0