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

Add new row e.row.DataBoundItem is null

5 Answers 980 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Nick Gebbett
Top achievements
Rank 1
Nick Gebbett asked on 03 Aug 2010, 07:48 PM
I have just realized that at CellValidating event e.row.DataBoundItem is allways null.
In Q1 2010 when I added a new row into the grid using add row, e.row.DataBoundItem wasn't null and contained a new entity with part filled properties.
Now my application fires many exceptions....

Is it a bug or feature?

5 Answers, 1 is accepted

Sort by
0
Nick Gebbett
Top achievements
Rank 1
answered on 03 Aug 2010, 07:50 PM
Just catched an interesting moment.
At RowValidating when I try to enter a new row e.RowIndex == -2
How can it be?
0
Nick Gebbett
Top achievements
Rank 1
answered on 03 Aug 2010, 08:22 PM
I'm really disappointed with Q2. If i will not fix all the bugs till 7AM, I will return back to Q1
0
Jack
Telerik team
answered on 04 Aug 2010, 06:41 PM
Hello Alexander Panfilenok,

Thank you for this question. 

As I said in your support ticket this is a desired change. We modified the new row behavior and now you should set directly row values. It is no longer needed to use the DataBoundItem property. The RowIndex property is valid only for data rows.  

I understand your concerns about our new version. Yes, it contains breaking changes, however we tried to keep them at minimum. We redesigned most of the code and provided many new features. If you could send us your application (in a new support ticket), we will do the transition to the new version for you.

I  am looking forward to your reply.

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
0
Nick Gebbett
Top achievements
Rank 1
answered on 05 Aug 2010, 07:54 AM
Thanks, I think i resolved the issue at least in first step.
In future I will refactor this to something better looking.

In the past I used CellValidationEvent to bind grid databound item to a local variable and if validation at Cell validation is ok and Row validation event is also ok, then save this variable to a database.

        void BaseGridView_CellValidating(object sender, CellValidatingEventArgs e)
        {
            if (e.Row == null) return;
            e.Row.ErrorText = String.Empty;
            if (BaseGridView.IsInEditMode && ((GridViewDataColumn)(e.Column)).FieldName != SelectFieldName)
            {
                e.Cancel = false;
                switch (((GridViewDataColumn)(e.Column)).FieldName)
                {
                    case "Name":
                        InvokeValidateEvent(new ValidationEventArgs(new WarehouseType() { Id = ((WarehouseType)e.Row.DataBoundItem).Id }, "Name", e.Value));
                        break;
                    case "Description":
                        InvokeValidateEvent(new ValidationEventArgs(new WarehouseType(), "Description", e.Value));
                        break;
                }
                if (Errors.Any())
                {
                    e.Cancel = true;
                    e.Row.ErrorText = Errors.ToList()[0].Message;
                }
                else
                {
                    ThereIsEntityToSave = true;
                    Entity = (WarehouseType)e.Row.DataBoundItem;
                }
            }
            BaseGridView.Refresh();
        }
During validation I had to create a new istance to make ESC button working currectly. (when user wants to restore the previous cell value)

Now, when databound item is null when a new row creating, I set the batabound item by code like this
                if (e.Row.Index < 0)
                {
                    if (e.Row.DataBoundItem == null)
                        e.Row.DataBoundItem = new WarehouseType();
                    NewRowAdding = true;
                }
                else
                {
                    NewRowAdding = false;
                }
and as validation doesn't bind e.value to a databounditem I use the line like this
                    case "Name":
                        InvokeValidateEvent(new ValidationEventArgs(new WarehouseType() { Id = ((WarehouseType)e.Row.DataBoundItem).Id }, "Name", e.Value));
                        if (NewRowAdding) ((WarehouseType)e.Row.DataBoundItem).Name = (String)e.Value;
                        break;
At the end I have noticed that some of the properties (that are selected using GridViewComboBoxColumn) are not displaying in the grid right after new row adding.

I used UserAddedRow and UserAddingRow events to find that.

During UserAddingRow all properties are ok but at UserAddedRow the properties, that were selected using GridViewComboBoxColumn are null,

I have fixed this in this way

        void BaseGridView_UserAddedRow(object sender, GridViewRowEventArgs e)
        {
            e.Row.DataBoundItem = Entity;
        }

where Entity is my local variable to save.

Thanks and sorry about my previous posts, I was a little bit angry =) I think this is understandable.
0
Julian Benkov
Telerik team
answered on 10 Aug 2010, 02:12 PM
Hi Alexander Panfilenok,

I am glad to hear that the issue is solved now. Do not hesitate to contact me back if you have further questions or issues.

Regards,
Julian Benkov
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
Nick Gebbett
Top achievements
Rank 1
Answers by
Nick Gebbett
Top achievements
Rank 1
Jack
Telerik team
Julian Benkov
Telerik team
Share this question
or