I have an ‘ObservableCollection’ that stores formatted ColumnLabels that need pasting into DataGrid.
The collection could have numerous different entries and can be of size ‘N’ therefore there will be ‘N’ number of columns in my DataGrid. Defined below:
private ObservableCollection<string> _stockColumnLabels;
public ObservableCollection<string> StockColumnLabels
{
get
{
if (_stockColumnLabels == null)
{
_stockColumnLabels = new ObservableCollection<string>();
return _stockColumnLabels;
}
return _stockColumnLabels;
}
set
{
_stockColumnLabels = value;
//OnPropertyChanged("StockColumnLabels");
}
}
Sample data this collection will hold e.g:
- “1”
- “2”
- “3”
- “4”
- “5”
N.B, all strings of course, essentially can be seen as a single row of data that represent columns when viewed on the GUI.
The collection is wrapped into a property for the sake of DataBinding and lives in the ViewModel and the DataGrid lives in the View. The DataContext of the View is set at RunTime when the App.cs file is run !
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow window = new MainWindow();
window.DataContext = new FSEnquiryViewModel();
window.Show();
}
}
GUI (VIEW)
At present due to the nature of the requirements being set on this project, the Column names have to be formatted out of ProductCode so to make life easier, I created one DataGrid that simply holds a single row of data which are column names and then another DataGrid just below that will hold row values. Don’t worry about the second DataGrid, I will sort that later, as the set-up is exactly the same. For now can someone help me ‘Bind’ my ObservableCollection found in my ViewModel to my DataGrid, it’s just a single strip of Data.
To Look Like
1 2 ... n <- 'DataGrid1'
Row1 | 1 | 3 | 5 | 6 | <- 'DataGrid2'
Row2 | 3 | 2 | 1 | 8 |
My code below hopes to answer the question that allows you to bind data from an unknown number of sources, each results could be unknown in size as well as the potential number of columns that could be used. Then shape the results slowly row by row into a dynamic collection which represents the final data layout.
Basically more or less the same as BlindMeis answer but he posted code that was syntactially incorrect and didn’t look like it had been tested before posting. Mine below is however code that I am currently using.
Below you will find what worked for me assuming you have a result set or sets to begin with.
Declare a table to hold your results.
DataTable shapedResultsTable = new DataTable();
Next thing, would be to filter some results from a DB call or procedure who results you are trying to re-shape. Something like:
Once you have a list of column names, add them to the your data table declare above.
Next, declare, instantiate and populate a list of unknown size because your results are of size n or amount n, to store your row values.
Next, repeat step 2 and 3 for every row of data you would like to show on your new Shaped Results Data Table, more over one for each row found in your result set.
Once you have a few collections that represent each row you want to add to your Data Table. Add them in the order you would want them to be displayed in.
shapedResultsTable.Rows.Add(totalsArray.ToArray());
shapedResultsTable.Rows.Add(branchRowValues.ToArray());
Finally, assign your DataTable to a ‘DataBinded Property’.
StockResultsTable = shapedResultsTable;
The property that is bound could look like:
Finally the xaml on the front end could look like:
I don’t know why the auto generate columns property is turned on but this works for me.
Steps 1-9 worked for me, working with a sample data set of results returned from a query or SProc, the steps above briefly outline what I did in displaying the results from the numerous queries I had to run to get very different data from many different SProcs but had a common underlying theme which allowed them to be displayed under common column names.
Be mindful of the fact that you will run into trouble where adding a row of data which has more members in it that the DataTable has columns. A DT which has 6 columns but you are adding a row which has come from a different Query altogether, which has 7 individual cells of data. This will not work, simply add in the new column that is not already there and try again.