I am trying to display items in a grid however the rows and the columns should be generated dynamically. I actually implemented a custom control that derives from Grid control and provide additional properties such as RowCount.
Here is a picture of the grid generated by my custom control using two dimensinal string array as datasource:
But i think my control consumes more than needed resources because it simply destroys column and row definitions and recreates them. Is there any simpler way to implement that control?
You don't need to create a custom control to do that for you... you can use a standard DataGrid. There have been a number of questions on displaying dynamic data in a DataGrid. Please take a look at some of these posts:
How do I bind a WPF DataGrid to a variable number of columns?
DataGridColumn Binding with Dynamically Generated Data
Visualizing 2D Data in a Table
Displaying multidimensional data in WPF
Related
I have two RadGridViews in my application. Two Grids are pulling the information from two different sources. That information bound to grid view.
Both the sources are of same Type List<MyComplexObject>. Now I wanted to compare these two grids, and color the cells (to Red) in second grid which has the different value.
How can we implement this functionality using Silverlight, C# and Telerik RadGridView?
You could use a Style Selector for the grid you want to have colored in a different way. You would need to prepare a normal cell style and a "Red" cell style as in this example. The article present the complete solution for cell style selector so you will have a full view on how this works. What you would need to do additionaly is to add a property (DependencyProperty) to the style selector to which you would bind the data collection against which you check the cell value validity.
I am assigning a datatable with multiple columns and zero rows to WPF data-grid. I am using auto-generated columns. As there are no rows to display the AutoGeneratingColumn event is not at all triggered. Because of the that it renders Data-grid in a weird manner: one single template row and now columns.
Is there any workaround for this problem? Please guide.
Regards,
Priyank
This is actually by design. The data grid uses reflection internally to infer the columns from the data type that is available in the ItemSource collection. When there are no items it is not possible for the data grid to display the column headers correctly.
There are two possible solution to this:
Bind your grid to a static resource. This way the grid will know the
clr type to which it will be bound and will correctly generate the
columns.
Do not rely on AutoGeneratedColumns ;)
I'm new to Silverlight, so this might be a simple question to answer. I have a grid (not DataGrid from the toolkit), so that data displayed in it can be easily tabulated. I want to bind the grid to a string array that never changes, hence the number of rows and columns in the grid will not change either. I see that I cannot specify a template like I can for a ListBox (ListBox.ItemTemplate) and I don't want to define each row and column explicitly (with a TextBlock in each one of them) in XAML. And I also dont want to generate rows and columns dynamically in code behind as it is not blendable. What's the best way to do this?
You might like this post
I have some data that I am displaying in a DataGrid. This data has multiple properties, which represent the columns of the DataGrid. One of these properties is a collection of another type of entity.
When a user selects an item in the DataGrid, I want to show the collection of entities in a tabular structure. In a sense, I'm looking to create a DataGrid inside of the RowDetailsTemplate of a DataGrid. I am not set on using a DataGrid, I'm just using this because it is the only kind of control I am aware of that can display tabular information.
My problem is, I do not know how to create the XAML that addresses this type of binding structure. Can anyone show me some code or point me to a code example?
Thank you!
I'm using the WPF DataGrid, and I'd like to know if there is any way I can access the DataGridRow's RowDetails programatically.
For example, when the user selects the row, I'd to grab some data from somewhere (say, a database), and display it in the RowDetails.
All of the examples I've seen tend to just display some extra bound data that isn't included in the columns for the row.
You could display some extra data but lazy-load it on SelectionChanged.
It usually is not easy to work directly with the WPF controls, they are not really meant to be used without a backing databound model.
If you have all the data in list of objects (or something similar) then you can do all sorts of fun things. I'm using a WPF Datagrid in this manner, and when a user selects a row, I think populate an editor control above the grid with the row item plus additional details.
That said, there's nothing stopping you from adding in additional information in the grid that's normally hidden and a trigger on row selection to show the additional data
you can use the following code
DataRowView row = (DataRowView)MyDataGrid.SelectedItem;
string strName = row.Row["Name"].ToString(); //where "Name" is the column name