Hi,
I've got the following issue:
My application is intended to show any table from the database. It means that the structure of the table is only known in run-time. I looked through the samples, but all the examples contain DataTemplate definition in XAML file that is not my case. I browsed the documentation but did not manage to fing the way how to create DataTemplate programmatically.
Are there any ideas how can I display a table when its structure is only known during runtime.
TIA

Displaying DataTable in run-time
kostrin
This link shows how to create a ControlTemplate in code, the same type of coding is used to create DataTemplates: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=805299&SiteID=1
With regards to your question about displaying a DataSet whose schema is unknown at compile-time, perhaps you might want to create a set of DataTemplates in a resource dictionary and add columns to a ListView at runtime. When adding a column for each DataColumn in the DataTable, load the appropriate DataTemplate according to the type of the column and set that as the column's template.
HTH
Blader
I cooked up some code(seems to work fine)
DataSet ds = new DataSet();
DataTable dt = new DataTable();
for (int i = 0; i < 2; i++)
dt.Columns.Add("column" + i.ToString());
for (int i = 0; i < 10; i++)
dt.Rows.Add(new string[] { "itema", "itemb" });
ds.Tables.Add(dt);
GridView gv = new GridView();
GridViewColumn gvc = new GridViewColumn();
gvc.Header = "column0";
Binding b = new Binding();
b.Source = ds.Tables[0];
b.Path = new PropertyPath("column0");
gvc.DisplayMemberBinding = b;
gv.Columns.Add(gvc);
GridViewColumn gvc1 = new GridViewColumn();
gvc1.Header = "column1";
b = new Binding();
b.Source = ds.Tables[0];
b.Path = new PropertyPath("column1");
gvc1.DisplayMemberBinding = b;
gv.Columns.Add(gvc1);
list1.View = gv;
b = new Binding();
b.Source = ds.Tables[0];
list1.SetBinding(ListView.ItemsSourceProperty, b);
mobigital
Again an issue with displaying data in runtime. Now it shows only the first row.
XAML:
<
ListView Name="viewTable" Margin="0,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" MinHeight="300"><
ListView.View><
GridView></GridView></
ListView.View></
ListView>Code:
// just some preparation from OnDataContexChanged where the DataSet is validated
if (!(DataContext is DataSet)){
throw new ArgumentException("Cannot set non DataSet object to DataContext");}
DataSet dataSet = (DataSet)DataContext; int idx = dataSet.Tables.IndexOf(mainTableName); if (idx == -1){
throw new ArgumentException("Cannot set DataContext as it has incorrect structure");}
mainTable = dataSet.Tables[idx];
// Generating Grid View
GridView gridView = new GridView(); GridViewColumn gridCol; foreach (DataColumn dataCol in mainTable.Columns){
gridCol =
new GridViewColumn();gridCol.Header = dataCol.ColumnName;
Binding binding = new Binding(dataCol.ColumnName);binding.Mode =
BindingMode.TwoWay;// this made bacause there should columns taken from other tyables within the same DataSet
binding.Source = mainTable;
gridCol.DisplayMemberBinding = binding;
gridView.Columns.Add(gridCol);
}
viewTable.View = gridView;
viewTable.DataContext = DataContext;
But when the grid is shown I can see only the first row. But I know for sure that there are more then 14000 records in the table. What can cause such an issue Thanks in advance.
ClaudeG
heimdaloz
Giedrius Banaitis
Lee, thank you very much for the spent time on my issue. I have just made the same
so we cannot use different DataTables within the same DataSet for the same GridView
Again thank you for the spent time!!!
WXS123
Thank you. But ListView.ItemsSource is supposed to implement IEnumerable interface, but neither DataSet nor DataTable do this :-(
cookieCutter
you can create the listview/gridview in the code behind, add the GridViewColumns at the runtime.
you can programmatically create a template from a string or a memory stream using the Load method of the XamlReader class.
FrameworkElementFactory is deprecated way to programmatically create templates
c_shah
thank you very much. It shows all the lines now, but they are the same :-(
I have made new solution with only project. then modified the only part of you code.
for (int i = 0; i < 10; i++)
dt.Rows.Add(new string[] { "itema" + i, "itemb" +i });
So I just made the items in the table different.
after starting the appication you can see all tyhe items the same (as the first row - itema0 itemb0).
If you select another one (e.g. the third), then all the items are like the selected row in the table (i.e. itema2 itemb2). do you have the same issue or it is an issue with my .Net framework version Thank you again in advance.
Simon Gittins
GridViewColumn gvc = new GridViewColumn();
gvc.Header =
"column0"; Binding b = new Binding(); // b.Source = ds.Tables[0];b.Path =
new PropertyPath("column0");gvc.DisplayMemberBinding = b;
gv.Columns.Add(gvc);