WPF: add dynamic ToolTip to DataGridCell - c#

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.

Related

WPF Datagrid restrict input with combobox

i am using the EF and the MVVM approach to get a bunch of data records in an ObservableCollection. This ObservableCollection lives in a ViewModel.
The ViewModel is than bound to a DataGrid. This works just fine.
For the sake of data consistency i now want to turn a bunch of columns in the DataGrid into ComboBoxes. So the user can only choose from a predefined set of values. These predefined values are also stored in the database (You can think of it as a lookup table). I can load the values from the database and also put it in an ObservableCollection. But now i wonder how can i bind this to the DataGrid, since i can only specify one ItemSource for the DataGrid.
Is this even the right approach in WPF?
Bind the ItemsSource property of DataGridComboBoxColumn to your values from the lookup table.
Then bind SelectedValueBinding to the field in the main collection.
If you are using normalised values (i.e. numeric values mapped to the items in the lookup table), then you should also set the SelectedValuePath and DisplayMemberPath properties.

Can I use a combobox in a programmatically bound datagrid C#

I am using entity framework and binding to a datagrid to display the results of my query. Instead of showing the foreign key from the result, I would like to show the more meaningful value from the lookup table and limit the users input by using a combobox too.
Is it possible to use a combobox in a programmatically bound datagrid on a winform? If so - how?
Thanks in advance,
James
There is a column type for a combobox in a DataGridView:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.aspx
For changing the display value you can use this event:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellformatting.aspx

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.

C# WPF datagrid: ItemsSource

I want to know how you can modify properties of columns of a WPF toolkit datagrid once the ItemsSource has been set (it is set in XAML)? For example I want to make a specific column have the property IsReadOnly equal to true.
So basically there are two things I want to know:
-How can I get access to a specific column once the ItemsSource has been
set?
-How can I change the properties of a specific column once the ItemsSource
has been set?
Answer to both questions is:
By looping through your datagrid
Access column by it's index

Dynamically Filter data in a DataGrid

I have a ComboBox with the values "open", "closed". According to values changed in the ComboBox, I want to change the DataGrid to display either "open " or "closed" values. How can I do this?
Your DataGrid can be bound to a DataView. Create different DataViews depending upon the Selected item in the DropDownList (here's an article on how to retrieve the selected item).
Fill a DataTable with your data. Derive different DataViews for the combo states. When the combo changes (enable AutoPostback), select the appropriate DataView and Bind the DataGrid.
You could use RowFilter property of a DataView object binded to the DataGrid.
You can use any of the *DataSource controls and add a control parameter with the ID of the combobox. Turn on autopostback for the combo, and asp.net will automatically call the object datasource with the new open/closed value.

Categories

Resources