This is a migrated thread and some comments may be shown as answers.

Column value depaned on related child table records

5 Answers 109 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Yael
Top achievements
Rank 1
Yael asked on 18 Sep 2008, 09:51 AM
Hi
I have 2 tables in a hieracycal grid.
I need to add a column to the parent table that it's value is calculated depanding on the chiled rows, like that:
In the child row there is a checkbox column:
 if all the child rows (of a specific parent row) checkbox = true, then the column value is: full
 if some of the rows checkbox = true, then the column value is: partly
 if all the rows checkbox = false, then the column value is: empty

Can you give me an idea, how to implement this?

Thanks Yael

5 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 19 Sep 2008, 03:40 PM
Hi Yael,

Thank you for this question.

You can do this by processing the ValueChanged event. Follow these steps:

  1. Check whether the changed cell is a GridCheckBoxCellElement and whether it is in the desired column.
  2. Get the parent row and iterate through all rows in the view.
  3. You should process the current row in a special way, because at the moment when the ValueChanged event is fired, the value isn't yet written. It is contained only in the active editor.
  4. Finally, set the desired value of the parent cell
The following code snippet demonstrates this behavior:

bool inValueChanged; 
 
void grid_ValueChanged(object sender, EventArgs e) 
    if (!inValueChanged) 
    { 
        inValueChanged = true
        GridCheckBoxCellElement cell = this.grid.CurrentCell as GridCheckBoxCellElement; 
        if (cell != null && this.grid.CurrentCell.ViewTemplate.Parent != null && 
            ((GridViewDataColumn)cell.ColumnInfo).FieldName == "checkbox"
        { 
            GridDetailViewCellElement parentCell = (GridDetailViewCellElement)cell.TableElement.Parent; 
            GridViewDetailsRowInfo detailsRow = (GridViewDetailsRowInfo)parentCell.RowInfo; 
            GridViewDataRowInfo parentRow = detailsRow.Parent; 
            GridViewRowInfo[] rows = cell.ViewTemplate.GetChildRows(parentRow); 
 
            bool allChecked = true
            bool containsCheckedRow = false
 
            if ((bool)this.grid.ActiveEditor.Value) 
            { 
                containsCheckedRow = true
            } 
            else 
            { 
                allChecked = false
            } 
 
            foreach (GridViewRowInfo row in rows) 
            { 
                if (row != cell.RowInfo) 
                { 
                    if (!(row.Cells["checkbox"].Value is System.DBNull) 
                        && (bool)row.Cells["checkbox"].Value) 
                    { 
                        containsCheckedRow = true
                    } 
                    else 
                    { 
                        allChecked = false
                    } 
                } 
            }                     
 
            if (allChecked) 
            { 
                parentRow.Cells["Status"].Value = "full"
            } 
            else if (containsCheckedRow) 
            { 
                parentRow.Cells["Status"].Value = "partly"
            } 
            else 
            { 
                parentRow.Cells["Status"].Value = "empty"
            } 
            parentRow.InvalidateRow(); 
        } 
        inValueChanged = false
    } 
 


I hope this helps. Should you have any further questions, I will be glad to assist you.

All the best,
Jack
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Yael
Top achievements
Rank 1
answered on 21 Sep 2008, 09:21 AM
Hi
Thanks for the reply, it is very hellpfull.
My problem is: the event 'ValueChanged' is not fired!
The reasone may be that i am using BindingSource, and i edit the row values through textBox and checkBox that is binded to the grid through the BindingSource and the row in the grid is not editable at all.
But i tried to use event :'cellFormating'. It is working most of the time, but i have a problem of refresh. The parent cell is not alwayes updated imediatly to the user with the value of: 'empty'/'full' etc...
Only after clicking on other rows in the grid and expanding the column
header(???), the parent cell is updated.
I added 'grid.GridElement.EndUpdate(true)' after the row:
parentRow.InvalidateRow();
It helps most of the time but there are still cases that the grid is not updated.

The problem of refresh is problematic also in other places:
1. when i update a column in the DataSet in other thread, the view is updated only when the mouse pointer is moving (not clicking) over the row.(In the Q1 release the grid was refreshed automatic, and ufter the upgrade to Q2 SP the grid is not update unless moving over the row with the mouse).
2. When i add a row and do
bindingSource.cancelEdit
bindingSourcebindingSource.ResetCurentItem
'grid.GridElement.EndUpdate(true)'
 the cancled row is still showing, only after changing something else in the grid (like expanding some of the headers ) the cancled row is disappered.

Another thing:
I have to say that your help does not contain such examples and this example is very hellpfull in the way of how to manage the child tables.

thanks, waiting for your answer!
yael
0
Jack
Telerik team
answered on 24 Sep 2008, 07:24 AM
Hi Yael,

Thank you for getting back to me.

The ValueChanged event is fired only when RadGridView is updated from inside. If you make changes outside the grid (e.g in a TextBox), you should notify RadGridView explicitly. The CurrencyManager does this when the current position was changed. In all other cases you should call the EndEdit method of your BindingSource and then the Update method of the CurrentView in the grid.

Consider the code snippet below:

void textBox1_KeyDown(object sender, KeyEventArgs e) 
    if (e.KeyCode == Keys.Enter) 
    { 
        myBindingSource.EndEdit(); 
        this.radGridView1.CurrentView.Update(GridUINotifyAction.StateChanged); 
    } 


I hope this helps. Do not hesitate to write me if you have further questions.

Kind regards,
Jack
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Yael
Top achievements
Rank 1
answered on 24 Sep 2008, 11:40 AM
Hi
I added to my code the following the event of checkBoxStateChange
and put the following lines:
       myBindingSource.EndEdit(); 
        this.radGridView1.CurrentView.Update(GridUINotifyAction)

Indeed the grid is updated and i see the checkBox value.
But the event ValueChanged is not fired.

The only way the event ValueChanged was fired is when i allowed updating the grid checkbox by setting:
grid.checkbox.ReadOnly = false;
And then the event ValueChanged was fired and the work of changing the parent cell status value.

The commands:
       myBindingSource.EndEdit(); 
        this.radGridView1.CurrentView.Update(GridUINotifyAction.StateChanged); 
Are very hellpfull. I wish i new it before!


For now i will use the cellFormat event. Is this OK?

Yael
0
Jack
Telerik team
answered on 24 Sep 2008, 10:35 PM
Hello Yael,

The CellFormatting event is fired every time when a cell needs to change its visual state. If there is no visible delay in the grid and the solution works for you, it is OK. If you send me your application I will be able to investigate the code and give a more concrete suggestion. You can do so in a support ticket.

If you have other questions, I will be glad to answer them.

Sincerely yours,
Jack
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
GridView
Asked by
Yael
Top achievements
Rank 1
Answers by
Jack
Telerik team
Yael
Top achievements
Rank 1
Share this question
or