Bind a GridView to a GridView object in a ViewModel - c#

I'm quiet new in MVVM, and I have a little probleme I can't solve.
I have a GridView and I would like to bind it to a GridView object in my viewmodel (which is separated from the code behind file of my XAML file).
But there's no attribute I can't write with {Binding ...}, and I dont want to give it in the arguments of the initialization of my view model.
I want to have access to all the properties of my GridView in the viewmodel, to get the selecteditem(s).
Thanks

The gridview class represents a view mode that displays data items in columns for a ListView control. About the only useful thing you can bind on the gridview is the Columns property.
If you want to bind rows for the gridview, bind at the listview level to the ItemsSource property.

Related

Setting datagrid columns headers

I have a datagrid (from WPF Toolkit) which has some columns.
Columns headers are set from xaml.
Then from view code-behind, there are places where I do below:
MyDg.Columns[10].Visibility = Visibility.Collapsed;
So the problem with this is that when I remove or add some columns to the datagrid then I need to modify the index set in the above expression depending on if the column I remove or add is before that referenced in the above expression.
So I would like to write the above expression in some manner to avoid the dependency with datagrid columns addition or removal process.
For example defining some constants in view code-behind for headers and from the view (xaml) assing these constants to datagrid as header names. Then from view code behind I could find the index from the column header and proceed:
var index = dataGrid.Columns.Single(c => c.Header.ToString() == "HeaderName").DisplayIndex;
dataGrid.Columns[index].Visibility = Visibility.Collapsed;
I also have thought about binding a property from view code-behind to each datagrid column header but in the view code-behind constructor I set the datacontext to the view model.
So any idea on how to do this?

WPF: add dynamic ToolTip to DataGridCell

I'm developing my first WPF application that queries a database and shows some records of some tables in TabControl if one or more fields of these records not satisfy certain condition.
I have a DataTable as data source and I use a DataGrid to show results (i.e. the wrong records). I'd like to use ToolTip on DataGridCell to indicate why a field is considered wrong. There's a way to iterate over the DataGridRow and the DataGridCell so that I can set dynamic ToolTipfor every specific field?
Thanks in advance.
I would bind the DataGrid selected item to a SelectedRecord property in my view model (where the data source is coming from), see Get selected row item in DataGrid WPF as an example. The SelectedRecord property would then set the SelectedRecordToolTip property in accordance to the SelectedRecord value (i.e. using a dictionary with the error as the key and the tooltip as the value). Finally, you can then bind your tooltip to the SelectedRecordToolTip property.

Set WPF Datagrid column as a Combobox itemsource

I have a Combobox, in which I would like its items to be the column data that is located on a DataGrid. Is there anyway to set the Combobox itemsource to be a specific column of a DataGrid?
Right now I'm iterating each row of the DataGrid, getting the field's data and adding them to the Combobox, but that means that I would have to clear all the items and reiterate everytime the DataGrid is modified.
You can set ItemsSource and DisplayMemberPath properties:
comboBox1.ItemsSource = dataGrid1.ItemsSource;
comboBox1.DisplayMemberPath = "ColumnName";
I Think you'are taking the wrong approach. Your data grid must be bound to a collection of object. I guess you could just build another collection by extracting the desired fields (for example with linQ) and expose this new collection to your view such that you can bind your combobox.
I you want to keep this second collection updated, make your first main collection an ObservableCollection such taht your can subscribe to CollectionChanged Event. In the event handler, just manage the add and remove in your combobox source collection.

Using converters for DataGrid Column

I create my windows databainding infrastructure with MVVM pattern. I have method which return data for my DataGrid. Problem that I want some columns in DataGrid use Converter (IValueConverter) but I bind my DataGrid to the data directly. I can't strongly type columns specification in xaml because number of columns can be different. Also I can't get UI element (DataGrid) in ViewModel for change.
Your ViewModel can expose the column collection (structure which can specify column header, type of data, necessary information to use converter) bind that to a DP of your view and your view needs to add the columns to the datagrid after parsing that and preparing datatemplate for datagrid column's cell template and cell edittemplate which specifies the binding source, converter.

Combobox and Gridview

If we select the dropdown list Item then I want to display the Item Particulars in the Gridview Please Help me
On the SelectedIndexChanged of the dropdown rebind your GridView off of the selected value
Add a dropdownlist control, a button control, a grid view and an sqldatasource.
Configure the datasource first in sqldatasource.
I would recommend write a storeprodure to fetch data into the gridview (sqldatasource).
Bound the parameter to "controls" and select the dropdownlist. Now your DDL is bound to the gridivew. In his table click on advance properties and change property convertemptystringto null = false. (there property is something similar).
If you want a button to update the grid view, do this. Add the default event control to the button, put no code in it.
If you want to change the grid view content based on select directly then choose autopost on selected changed in properties of the DDL.
If everything is fine, this should populate your grid view based on contents in DropDownlist

Categories

Resources