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

Disappearing Data

3 Answers 287 Views
GridView
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 18 Mar 2010, 06:52 PM
I have created a GridView to display a parent-child dataset. I have created and assigned all columns in code. This code works fine. I am able to add data and successfully update the database.

I need to capture changes to a GridViewComboBoxColumn. I want to detect when the ComboBox selection has changed and set date values for other fields in the row. I have included the event code below. It mostly works. The values come back to the grid in the correct place, but as soon as I click into another cell, the values disappear.

I have also tried a solution similar to the code below using rgv.CurrentRow.Cells["From"].Value instead of CellElement. The behaviour and results are similar but not quite successful. I have also tried enclosing the cell updates inside of rgv.GridElement.BeginUpdate() and rgv.GridElement.EndUpdate(); That didn't work either. .

This is a pretty typical thing to do in Microsoft .NET controls so I am a bit mystified as to why this appears to be so difficult with advanced controls. I am certain it is user error. but I just don't know where to look at this point. .I would appreciate it immensely if someone could point me in the right direction.

I am new to Telerik. I have figured out quite a bit in the last few days, but now I am under the gun to deliver.

Thanks in advance
John


private void rgv_ValueChanged(object sender, EventArgs e)
        {
            if (sender is RadComboBoxEditor)
            {
                RadComboBoxEditor editor = this.rgv.ActiveEditor as RadComboBoxEditor;
                if (editor != null)

                switch(editor.Value.ToString())
                {
                    case "Before" :
                        rgv.CurrentRow.Cells["From"].CellElement.Text = DateTime.MinValue.ToString();
                        rgv.CurrentRow.Cells["From"].CellElement.Enabled = false;
                        rgv.CurrentRow.Cells["To"].CellElement.Enabled = true;
                        break;
                    
                    case "After" :
                        rgv.CurrentRow.Cells["To"].CellElement.Text = DateTime.MaxValue.ToString();
                        rgv.CurrentRow.Cells["From"].CellElement.Enabled = true;
                        rgv.CurrentRow.Cells["To"].CellElement.Enabled = false;
                        break;
                    
                    case "Range" :
                        rgv.CurrentRow.Cells["From"].CellElement.Text = DateTime.Now.ToString();
                        rgv.CurrentRow.Cells["To"].CellElement.Text = DateTime.Now.ToString();
                        rgv.CurrentRow.Cells["From"].CellElement.Enabled = true;
                        rgv.CurrentRow.Cells["To"].CellElement.Enabled = true;
                        break;
                    
                    default :
                        break;

                }
            }


3 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 23 Mar 2010, 03:07 PM
Hi John,

Thank you for contacting us. The ValueChanged event fires when an editor changes its value and before saving this value in the cell. The cell value is changed when closing the editor. You should handle the CellValueChanged event instead. Also, RadGridView uses virtualization for its visual elements. Only the rows that are currently visible have corresponding visual elements. When scrolling, these elements are reused. This is why you can't set the cell element text directly. You should handle the CellFormatting event. I modified the code accordingly:

void  radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    GridViewDataColumn dataColumn = e.CellElement.ColumnInfo as GridViewDataColumn;
    if (dataColumn != null)
    {
        string comboValue = e.CellElement.RowInfo.Cells["combo"].Value.ToString();
 
        if (dataColumn.UniqueName == "From")
        {
            if (comboValue == "Before")
            {
                e.CellElement.Text = DateTime.MinValue.ToString();
                e.CellElement.Enabled = false;
            }
            else if (comboValue == "After")
            {
                e.CellElement.Enabled = true;
            }
            else if (comboValue == "Range")
            {
                e.CellElement.Text = DateTime.Now.ToString();
                e.CellElement.Enabled = true;
            }
        }
        if (dataColumn.UniqueName == "To")
        {
            if (comboValue == "Before")
            {
                e.CellElement.Enabled = true;
            }
            else if (comboValue == "After")
            {
                e.CellElement.Text = DateTime.MaxValue.ToString();
                e.CellElement.Enabled = false;
            }
            else if (comboValue == "Range")
            {
                e.CellElement.Text = DateTime.Now.ToString();
                e.CellElement.Enabled = true;
            }
        }
    }
}

However, this will change only the visual appearance of the cell. If you want to change the cell value, you have to handle CellValueChanged event:

void radGridView1_CellValueChanged(object sender, GridViewCellEventArgs e)
{
    GridViewDataColumn column = e.Column as GridViewDataColumn;
    if (column != null && column.UniqueName == "combo")
    {
        switch(e.Value.ToString())
        {
            case "Before" :
                e.Row.Cells["From"].Value = DateTime.MinValue.ToString();
                break;
             
            case "After" :
                e.Row.Cells["To"].Value = DateTime.MaxValue.ToString();
                break;
             
            case "Range" :
                e.Row.Cells["From"].Value = DateTime.Now.ToString();
                e.Row.Cells["To"].Value = DateTime.Now.ToString();
                break;
        }
    }
}

I hope this helps. If you need further assistance, I will be glad to help.

Regards,

Jack
the Telerik team

 


Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
John
Top achievements
Rank 1
answered on 23 Mar 2010, 04:40 PM
I have added the code to handle the CellValueChanged event which seems to work for existing rows. It does not fire the event for new rows until I leave the combobox. And when the event does fire the FROM and TO dates are not updated. I have handled the DefaultValuesNeeded event thinking it might help and while it does put default data in the new row the CombobBox change logic that I want to execute DOES NOT execute for the new row.

I have been working with .NET since before it went GA but I am finding this very frustrating. I am trying to pilot this control set for what I thought was a relatively simple project. While I need an answer to this issue, is there a control life cycle you can point me to so I understand the event flows, hierarchies, and triggers?
0
Jack
Telerik team
answered on 24 Mar 2010, 02:11 PM
Hi John,

CellValueChanged event fires after closing the editor, when saving editor's value in the current cell. If you want to be notified for every change in the combo box, you should handle the ValueChanged event. You can find more about editor events from our online documentation: EventsHandling Editors' Events.

In order to support themes and animations we designed our control set following the WPF design. So, just like in WPF, each of our controls contains a hierarchy of elements. Maybe the following help article will give you a better picture of our Telerik Presentation Framework. If you have any further questions, I will be glad to help.

I am not sure that I understand correctly your scenario. So, please send me your application and I will try to find a solution.

Sincerely yours,
Jack
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
GridView
Asked by
John
Top achievements
Rank 1
Answers by
Jack
Telerik team
John
Top achievements
Rank 1
Share this question
or