How to Modify Data Grid Cell Back Ground from code behind? - wpf

I would like to modify the background of Data Grid Cell, but only Row Header Value and Column Header Value information was available.
I have tried to implement the approach in the below link, but nothing worked.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/63974f4f-d9ee-45af-8499-42f29cbc22ae/grabing-controls-from-a-datagrid?forum=wpf
How to get the Data grid Cell Object by using Row Header and Column Header value.?

There could be multiple ways by which you are reaching your DataGridCell. Eg; By clicking a cell with mouse, or using a Row index and Column index or something else.
1> If you clicking a cell, then you have to traverse the visual tree upwards. And use the following approach :
DETECTING THE COLUMN, CELL AND ROW THAT HAS BEEN CLICKED .
2> You can use following approach :
object item = dgrdInvoice.CurrentCell.Item;
DataGridRow row = dgrdInvoice.ItemContainerGenerator.ContainerFromIndex(0);
DataGridRow row = dgrdInvoice.ItemContainerGenerator.ContainerFromItem(item);
dgrdInvoice.CurrentColumn.GetCellContent(row);
Note : Approach differs depending upon your particular scenario. As finding cell in general needs visual tree traversing.
3> For all other general scenarios, you can refer here :
Programmatically Selecting and Focusing a Row or Cell in a DataGrid

Related

Is there a way to change someDataGridViewCell.Style.BackColor (&etc) at any time?

Documentation says it must be done inside someDataGridView.CellFormatting event, but how can you pass the needed color information into it and force CellFormatting to be triggered when needed ?
someDataGridViewCell.Value is working fine for changing cell text from a System.Windows.Forms.Timer Tick event, but I can't seem to change the coloring at the same time.
In my case, all the columns are built and changed dynamically from user menus, and there can be dozens of them.
This is VS2015.
Update:
My issue was I used Color.FromName() which returns Empty (transparent) for misspelled names -- very very bad for grid background colors. (I required coloring in data streams I was using so name seemed handy.)
CellFormatting event used in most examples assumes you can derive the color information from the visible cell contents which is too limiting.
Using the CellFormatting event is recommended if you are changing colors on the datagridview on the Load event of the Form. Once the form has been Shown you can modify the cells in any method.
How you would do it would depend on how you iterate through the view and what you're trying to accomplish. Since you didn't specify VB or C# I'll put both.
Single Cell:
VB:
dgv.Rows(index).Cells(index).Style.BackColor = Color.PickAColor
C#:
dgv.Rows[index].Cells[index].Style.BackColor = Color.PickAColor;
Entire Row:
VB:
dgv.Rows(index).DefaultCellStyle.BackColor = Color.PickAColor
C#:
dgv.Rows[index].DefaultCellStyle.BackColor = Color.PickAColor;
Entire row while through the rows:
VB:
For Each row As DataGridViewRow in dgv.Rows
row.DefaultCellStyle.BackColor = Color.PickAColor
Next
C#:
foreach(DataGridViewRow row in dgv.Rows)
{
row.DefaultCellStyle.BackColor = Color.PickAColor;
}
Essentially if you're styling a single cell use the Cell.Style.BackColor and if you want to do the entire row then use the Row.DefaultCellStyle.BackColor

WPF Data gird row selection change event from selection change event

I'm looking for a workaround to an issue I've noticed in a project that I'm working on. I have a datagrid bound to a list of a class I've created. Whenever a user types in a stock symbol in this datagrid I have the roweditending event set to pull in some information to populate the other fields in the datagrid.
This generally works fine, however I've noticed that the event doesn't fire in some instances. I'm going to try to explain this the best I can... upon data entry if I enter information into a cell and then click another row on the datagrid - focus doesn't shift to the new row but shifts to the row containing the cell I was just entering data into from that cell, then if I select another row or input element the row edit ending event doesn't appear to fire and as such my code to populate the rest of the row doesn't execute.
Everything else is fine, and it's a pretty subtle issue, only one path of input to make it happen, but I was wondering if anyone had run into this or found a work-around?
I've been looking at the selection change event, but I need to be able to grab the row that was selected. In the row edit ending event I use
Dim NewTrade As Trade = e.Row.DataContext
to get the instance of my class that the row represents and to add the additional fields.
I'm not sure I see a way to get the row and it's datacontext for the row I just shifted from using the SelectionChangedEventArgs.
Any ideas for a workaround?

XAML - Access an array value from a template, indexed by the containing column position

I have an desktop WPF application that shows values from different files, the objective being to compare those files. The values extracted from the files are displayed in a DataGrid, one column per file. The user can add or remove files at will, which means that the amount of columns displayed in the grid can change. Columns are created and removed from a ViewModel, and have complex content that requires the use of DataGridTemplateColumn instances.
The data source is basically a two dimentionnal array (something like ObservableCollection<ObservableCollection<CellViewModel>>).
To fill my cells I need a data template, but the problem is that the DataTemplate's data source will be the row. And if I want to display the correct value, I'll have to know the index of the current cell from the template, so I access the correct index in my row. In the end, I'd like to be able to have a binding in the template that looks like {Binding Path=[someIndex].DisplayText} (DisplayText being a property of my CellViewModel).
I hope I've been clear enough :)
So the question is : how do I read the correct index in my row from the template ? Or, is there some better way to handle this variable-sized templated columns list problem ?
Thanks

CheckBox in Header (Fixed) row of C1.Win.C1FlexGrid grid

My question is "Is there a way to use a CheckBox inside a fixed row cell in a C1.Win.C1FlexGrid?".
I have a C1FlexGrid with two Fixed rows. (It is important to mention here that I am using the C1.Win.C1FlexGrid grid and not the WPF, or SilverLight version)
The first fixed row I have is used for the headers as usual. the second one though is customized to perform some other tasks all is working fine except for one task I can't get done. I need to use a CheckBox inside one cell of the cells of the second Fixed row (just like any Boolean cell in the grid's normal rows) because I want to use this CheckBox to Check/Uncheck all check boxes in the same column.
Of course setting the column datatype to bool won't do the job for fixed rows. Setting the editor of the cell to a CheckBox won't do also as the editor won't be visible at all times but only when cell is selected. Also, based on my research, there is a CellFactory property that some threads are discussing which can be used to do this job, but CellFactory is not implemented in C1.Win.C1FlexGrid class but only in WPF, SilverLight and Phone versions of the grid.
Any ideas on how to do this?
Create a new CellStyle with a Boolean DataType and set it to any Cell that you need. Here’s the code to implement it, assuming the cell is in Row 1 and Column 1:
//Implement 2 fixed rows
c1FlexGrid1.Rows.Fixed = 2;
//create and set a new style to the reqd. cell
var cs = c1FlexGrid1.Styles.Add("Boolean");
//set DataType
cs.DataType = typeof(Boolean);
//Set any alignment
cs.ImageAlign = C1.Win.C1FlexGrid.ImageAlignEnum.CenterCenter;
c1FlexGrid1.SetCellStyle(1, 1, cs);
Thanks,
Richa

writing a grid that has both column and row virtualization

I need to write an excel-like grid that can have a lot of cells (400x400). All columns have the same width and all rows the same height. Each cell can contain text or be empty and each cell can have a column and/or row span. I suppose this will never work with the Grid panel and I suppose I will need UI virtualization in both column and row direction.
So my first try was to create a virtualizing grid by deriving from VirtualizingPanel and implement IScrollInfo. This could have "easily" be the solution except that I ran into a problem:
To provide IScrollInfo with the relevant information about scroll size and position and to be able to detemine wich items need to be created (realized) next using the ItemsContainerGenerator, I need to know the column index, row indeox and columnspan for each child item (cell). The only way I can think of to do this is using attach properties. The problem is: I can only read the values of attached properties if the ItemContainer that has them is already realized. So I am in a catch 22 here. To know what to realize I need to realize all items. To provide the data for IScrollInfo I need to realize all items.
So it seems that I am at a dead end with this approach.
Do you have any idea how I could implement a control like this or know how I could reslove the above problem?
It strikes me that you may not need to instanciate the UI elements themselves- you can very easily have a collection of DependencyObject-derived viewmodels, each of which has the WidthProperty and HeightProperty set (and possibly bound to the equivalent Width and Height properties of the visible cell UI element, once those are created).
Storing 160,000 (400x400) class instances shouldn't be a problem, especially if you are able to index into the set using row and column.

Resources