I have a Winforms grid in C# that has some interesting requirements.
My grid has 3 columns...2 of them being drop downs...the other being a free form text field that is disabled when the row is initialized.
COLUMN1 (using a drop down) has the numbers "1-9" in it that a user can select. This in effect lets the user assign groups to the records on the grid.
COLUMN2 is using a drop down has 4 options (A/B/C/D) that a user can select. If the user selects "A" then I need to enable the cell in that row for COLUMN3 so they can enter a numeric value. If the user selects "B" then I need to enable the cell in that row for COLUMN3 so they can enter a percentage value. If they select "C" or "D" then the cell for COLUMN3 is disabled and any entered value is wiped out.
What makes it even more interesting is that if the row in question is part of a group (see COLUMN1) then I need to effect perform the same behavior (enabling or disabling COLUMN3) for each row in that group. For example...there are 6 rows in my grid that are assigned to group #2. If the user selects option "A" in COLUMN2, then I need to enable COLUMN3 to receive a numeric value for all 6 rows.
I know this is a tad confusing, but this is per user requirements. It would be nice to be able to set the background color of any disabled column cells to make things easier to understand for the user.
Is all this possible? If so, any good code snippets? I am currently using the CellChange event of the grid to determine what is being selected in COLUMN2. It's just all the other stuff that I am unsure of.
Thanks in advance for all your help. Much appreciated.
Sure this is possible. There's really nothing to it.
What I would do is handle the CellChange event. You can trap for changes to cells in Column 2 and based on the cell's Text (you must use Text in CellChange, you can't use Value), you can set the Activation property on the cell in Column 3 of the same row.
You can also check the parent row of the row being changed and check to see if IsGroupByRow is true. If so, then you know the row is in a group. You can then get the row's ParentCollection and loop through all the rows in the collection and modify those rows any way you like such as changing cell values or the Activation property of the cells.
One potential problem you might run into is that changing the value of a cell in another row might cause the CellChange event to fire again. I'm not sure if this is the case, but if it is, you will need to use the EventManager on the grid to disable the CellChange event while you are in it and re-enable it when you are done, so you don't get infinite recursion.
I got it! There were a few properties I weren't of aware of that made this pretty easy.