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

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

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

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

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

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.

Totals Row in a DataGridView

I am developing a winform application application. I wanted to show sum of columns in last row of each column. This row must always be visible.
At moment I am thinking about adding another datagridview just beneath my datagridview with records, and would show the sum in that bottom datagridview.
If there is a better way to achieve this task?
No, need of adding another datagridview
Solution 1: Please refer to this solution
Solution 2: If the above link is not exactly what you want then,
You can try to manually add last, summary, row in which you can display information that you need. For example, you can try to do the following:
Read data from database and fill System.Data.DataTable
Add one column to the newly created DataTable – that column might be set
to true for the last, summary, row
Programmatically add one extra row that contains suitable summary data
Do the data binding to DataGridView control
Using appropriate event, bold or otherwise graphically distinct summary
row (row that have extra column value
set to true)
You can do it in the same way as you suggest, like placing a datagridview for displaying the sum. you can also handle the Horizontal Scroll with this, if there are more columns.
Another method is there in this link
Another way you can Add Rows to your datasource itself to display the sum.
Even if this question is quite old-ish I'd like to propose an extension to Niraj Doshis answer. His answer holds true for a data bound DataGridView. I recently had the problem to calculate the summary in a user-editable DataGridView, whose solution differs in the details. Anyway It's quite straight-forward too. I am writing my functions on a higher abstraction level, which outlines the workflow, but elides the implementation details.
First of all you'll have to initialize the DataGridView, see
private void InitializeDataGridView()
{
SetColumnTypes();
AddEmptyRow();
AddSummaryRow();
}
I set DataGridView.AllowUsersToAddRows to false for the new row would be located beneath the summary row. Hence I am adding an empty row, which the user may fill with his data. The summary row is set to ReadOnly since we do not want our user to edit it. Whenever a CellEndEdit is raised I am updating the DataGridView with the following method
private void UpdateDataGridView()
{
RemoveSummaryRow();
RemoveEmptyRows();
UpdateRowNumbers();
AddEmptyRow();
AddSummaryRow();
}
First I remove the summary and all empty rows (you'll have to take care. If you are trying to delete the row you just edited an exception will be thrown. I've not yet figured out how to do this, but I just made up the solution. I'll amend when I came up with the solution.) Afterwards I'm setting a running number in each of the rows. This is not really required, but a detail of my implementation. At the end I add an empty row again which the user may use to add further data and then calculate and add the summary row.
As I said before this is not yet a ready-made solution, but rather I concept which works but with some quirks and bugs.

WPF UI automation - Doesn't support rows? (or how can I select and deselect an entire row)

Note: This is no longer an issue, the .NET 4 built-in DataGrid solves this problem
I have a WPF app which is using a DataGrid; I'm using the WPF ui automation API to write some automated tests for it.
The DataGrid is the WPFToolkit one, I'm using the .NET 3.5SP1 with VS2008, and, the datagrid has multi-select enabled.
Where I'm at is that from my test can find the datagrid, and I can find individual cells within the grid using the GridPattern.GetItem method, and select them by setting SelectionItemPattern.Select. Method
The code looks somewhat like this:
AutomationElement mainGrid = // find the grid in the window
var columnCount = (int)mainGrid.GetCurrentPropertyValue(GridPattern.ColumnCountProperty);
var mainGridPattern = (GridPattern)mainGrid.GetCurrentPattern(GridPattern.Pattern);
var rowToSelect = 2;
// select just the first cell
var item = mainGridPattern.GetItem(rowToSelect, 0);
var itemPattern = (SelectionItemPattern)item.GetCurrentPattern(SelectionItemPattern.Pattern);
itemPattern.Select();
This seems to work, but it only selects the first individual cell, not the entire table row (which has 10 columns) but I can't work out how to deselect an item. The only thing I can find that looks like it might work is to call SelectionItemPattern.AddToSelection() on the itemPattern, or the corresponding RemoveFromSelection but when I do either of these, the following exception is raised:
=> Cannot change cell selection when the SelectionUnit is FullRow.
at MS.Internal.Automation.ElementUtil.Invoke(AutomationPeer peer, DispatcherOperationCallback work, Object arg)
at MS.Internal.Automation.SelectionItemProviderWrapper.AddToSelection()
The underlying root issue appears to be that (as far as I can see) the WPF UI automation API has no concept of a grid Row, only cells. This seems somewhat problematic - Is this right?
Sidenote: I had previously been using the White UI automation framework - this doesn't use UI automation to select grid rows, instead it moves the mouse to the row location and clicks it - which caused random failures of our tests - is this why they're using the mouse to do the selection though?
If you use UISpy to look at the structure of your DataGrid as UIAutomation sees it, you'll notice that the DataGrid element contains a RowsPresenter, and the RowsPresenter contains a number of DataGridRows, each containing a DataGridCell.
I suspect what's happening is that var item = mainGridPattern.GetItem(rowToSelect, 0); is returning the item representing the cell, whereas you want the item representing the whole row.
You can get this by calling item.CachedParent - then Select() that.
This now seems to have been fixed in .NET 4. Rows are now exposed as real objects that support the SelectionItemPattern, so you can now select/deselect a row. Hooray

Resources