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

Problem while updating the Cell from Item Template of Herarchical Grid.

18 Answers 373 Views
GridView
This is a migrated thread and some comments may be shown as answers.
SachinC
Top achievements
Rank 1
SachinC asked on 07 Oct 2010, 12:55 PM
Hello,

I am using Radgrid.

i am adding two columns in gridview master template
With gridViewChecklistItems.MasterTemplate.Columns
           .Add(colChkListItemGroupID)
           .Add(colChkListtemGroupName)
       End With

i have added on template in Master template and adding some columns in it.
gridViewChecklistItems.MasterTemplate.Templates.Clear()
Dim itemsTemplate As GridViewTemplate = New GridViewTemplate
With itemsTemplate.Columns
            .Add(colChklistHeadingID)
            .Add(colChklistItemID)
            .Add(colChklistItemName)
            .Add(colChklistItemNote)
            .Add(colChkItemTypeName)
            .Add(colChkItemTypeSize)
        End With
  
        itemsTemplate.DataSource = gridListOfItems
        gridViewChecklistItems.MasterTemplate.Templates.Add(itemsTemplate)

and also added the relation with master template colum and child template column
Dim itemToGroupRelation As GridViewRelation = New GridViewRelation(gridViewChecklistItems.MasterTemplate)
       With itemToGroupRelation
           .ChildTemplate = itemsTemplate
           .RelationName = "GroupItems"
           .ParentColumnNames.Add("ChecklistHeadingID")
           .ChildColumnNames.Add("ChecklistHeadingID")
       End With
       gridViewChecklistItems.Relations.Add(itemToGroupRelation)


when i try to update the cell from mastertemplate column. the updated value in cell persists and also update the database
here i am using gridViewChecklistItems_CellValidating event to update the database. so its working fine.

now when i expand the item template/ child grid of specific row and update any sepecific cell,
the updated cell value does not persist in the Cell when i click on other cell or loose the focus from cell.
what would be the problem??.but the "gridViewChecklistItems_CellValidating" event is
fired and database is updated.


again one thing
when i update the one cell or more from itemtemplate. the event must be fired only once not for the cells
how to??


Thanks!









18 Answers, 1 is accepted

Sort by
0
SachinC
Top achievements
Rank 1
answered on 07 Oct 2010, 04:03 PM
any Help??
0
Emanuel Varga
Top achievements
Rank 1
answered on 07 Oct 2010, 04:10 PM
Hello Sachin,

I have a few questions before i can offer you a solution,
1. Are you using a bound? unbound grid?
If you are binding the grid what are you using as a data source? DataSet? Lists? BindingList?
What data source are you using for the ChildTemplate?

Please answer these questions and i will try to help you find a solution.

Best Regards,
Emanuel Varga
0
SachinC
Top achievements
Rank 1
answered on 07 Oct 2010, 05:08 PM
Hello Emanuel,

Thanks for your reply.

qnswers for your questions
i am using bound grid
i have added all the columns to master template and gridviewtemplate programatically.

i am using LInq to retrive data from entity framework and store it in List and bind this list to itemtemplate.datasource

This is for masterTemplate:
  

 

gridViewChecklistItems.MasterTemplate.Templates.Clear()
Dim listOfHeading = oCLBl.GetChecklistItems(CurrentCompany.CompanyID)
gridViewChecklistItems.DataSource = listOfHeading

 

 


This is for master template:

Dim listOfChkListItems = oCLBl.GetChecklistItem(CurrentCompany.CompanyID)
        Dim gridListOfItems = (From i In listOfChkListItems _
                               Select i.ChecklistHeading.ChecklistHeadingID, _
                              i.ChecklistItemID, _
                              i.ChecklistItemName, _
                              i.ChecklistItemNote, _
                              i.TypeName, i.TypeSize).ToList

itemsTemplate.DataSource = gridListOfItems

gridViewChecklistItems.MasterTemplate.Templates.Add(itemsTemplate)


is this information sufficient??


Thanks

0
Emanuel Varga
Top achievements
Rank 1
answered on 07 Oct 2010, 05:58 PM
Hello Sachin,

First i want to apologize to Sachin, for the misspelling of your name on the other thread last night, i guess i was to tired, sorry it was really a mistake :(.

But if you are using a bound grid, why did you create the columns programatically, you could have just bounded the grid, and register for the DataBindingComplete, and do other operations, like change columns headers, move columns around and other things.

Please try registering for CellEndEdit and try updating the values programatically and see if that works, like this:
void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e)
{
    var obj = e.Row.DataBoundItem;
 
    // update the object here based on the values from e.Row.Cells[]
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
SachinC
Top achievements
Rank 1
answered on 07 Oct 2010, 06:59 PM
Hello Emanuel,

its ok! it happens somtimes
:)

and  Thanks for your suggestion.
there are some other reasons to add the columns programtically.
and i dont have any problem for updating the db.
the problem is with cell in item template.
if i click on cell it  opens  for edit mode , i change the  value for example test to test1234 and when click on other cell or some where/ or press tab and move next the new value test1234 is discarded an old value test remains as it is.

it happens only for cells in GridViewItemTemplates.
the same senario is working with MasterTemplate cell.

Thanks

0
Accepted
Paul Patterson
Top achievements
Rank 1
answered on 07 Oct 2010, 07:26 PM
It's an Anonymous type issue. I don't think RadGrid likes Anonymous type lists when it comes to relationships and hierarchical templates. Maybe I'm wrong?

Try this...

Dim gridListOfItems = (From i In listOfChkListItems _
                             Select i.ChecklistHeading.ChecklistHeadingID, _
                             i.ChecklistItemID, _
                             i.ChecklistItemName, _
                             i.ChecklistItemNote, _
                             i.TypeName, i.TypeSize).ToList()
 
       Dim bindingItemsList As New BindingList(Of ChecklistItemListItem)
       For Each item In gridListOfItems
           bindingItemsList.Add(New ChecklistItemListItem(item.ChecklistHeadingID, _
                                                          item.ChecklistItemID, _
                                                          item.ChecklistItemName, _
                                                          item.ChecklistItemNote, _
                                                          item.TypeName, _
                                                          item.TypeSize))
       Next

...Where ChecklistItemListItem is the class that you can create the binding list with.

Worked for me.

Cheers!

0
Emanuel Varga
Top achievements
Rank 1
answered on 07 Oct 2010, 07:37 PM
Hello guys,

Honestly it never happened to me, I've been using Lists<>, BindingLists, DataTables, DataSet, and never had this problem, but i still think that an easy fix would be just to handle the CellEndEdit event, get the DataBoundItem and assign cell values to the properties, like:
void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e)
{
    if (this.radGridView1.Templates[0].Rows.Contains(e.Row))
    {
        var car = e.Row.DataBoundItem as Car;
        car.ID = Convert.ToInt32(e.Row.Cells["ID"]);
        car.Model = e.Row.Cells["Model"].ToString();
    }
}

But honestly I am extremely curios how and why this happens? I created 3 or 4 different examples of hierarchical binding just this afternoon and i cannot replicate this behavior...

So if someone can create a small form, and put it here i would be more than happy to help more.

Best Regards,
Emanuel Varga
0
Paul Patterson
Top achievements
Rank 1
answered on 07 Oct 2010, 08:47 PM
Emanuel, here you go... (click here)

The solution is; Visual Studio 2010, SQL 2008 R2, using RadControls for WinForms 2010 Q2.

Paul
0
Emanuel Varga
Top achievements
Rank 1
answered on 07 Oct 2010, 08:54 PM
Great, I'll get back to you as soon as i come up with something,

Best Regards,
Emanuel Varga
0
Emanuel Varga
Top achievements
Rank 1
answered on 07 Oct 2010, 09:40 PM
Hello again,

Paul you were right it is an issue with anonymous types, but i will dig a little further to see what i come up with, but until then, i would just return the query result as a specific type, see this msdn article,
so, this code:
Dim gridCarItems = (From c In DataContext.DBEntities.Cars.Include("Model") _
                   Select c.Model.ModelID, c.CarID, c.CarName, c.CarColor).ToList
would turn to this:
Dim gridCarItems = From c In DataContext.DBEntities.Cars.Include("Model") _
                   Select New ModelCarListItem With { _
                        .ModelID = c.Model.ModelID, .CarID = c.CarID, .CarName = c.CarName, .CarColor = c.CarColor}

I cannot see any downside and it's better than creating a new list, going trough all the items and so on.

Please let me know what you guys think,

Best Regards,
Emanuel Varga
0
Paul Patterson
Top achievements
Rank 1
answered on 07 Oct 2010, 10:22 PM
Thanks Emanuel. I'll keep digging on my end too.

Cheers.
0
Emanuel Varga
Top achievements
Rank 1
answered on 07 Oct 2010, 10:36 PM
The question is, is this a good enough solution in your case or not?

Sachin, what are you using to bind the Grids data source? I'm guessing an IQueryable Entity of Type sometype, if this is the case this is the reason why it is working.
This will not work, now you will not be able to change any value:
RadGridView1.DataSource = (From m In DataContext.DBEntities.Models
                 Select
m.ModelID, m.ModelName).ToList

But this will work:
Private Function GetModels() As IQueryable(Of Model)
    Return From m In DataContext.DBEntities.Models
End Function

So, in conclusion the grid will bind to anonymous types, but you will not be able to edit them. The value is the same (not chaning) on all the events I've tried CellValueChanged, CellEndEdit

Hope this helps, please let me know if there is anything else i can do to help.

Best Regards,
Emanuel Varga
0
SachinC
Top achievements
Rank 1
answered on 08 Oct 2010, 07:39 AM
Thanks Paul!!
Thanks Emanuel

For your time.

Cheers!
0
Emanuel Varga
Top achievements
Rank 1
answered on 08 Oct 2010, 07:45 AM
Hello Sachin,

I'm glad you got your answer but i would suggest to be careful if you will have large amounts of data it might become very slow creating one anonymous list and then go trough all of the elements and creating a binding list to which you will add all the elements one by one...

Best Regards,
Emanuel Varga
0
Emanuel Varga
Top achievements
Rank 1
answered on 10 Oct 2010, 09:32 PM
Hello guys,

I remembered to try something when we were discussing this problem. The same problem exists with the standards DataGridView from Microsoft, but that grid turns to ReadOnly when you bind it to an anonymous data source, so not even Microsoft has a good solution for this problem.

Just wanted to let you know, Sachin i would suggest using a different approach when binding data to the grid, i know it's easier with anonymous types but the .NET language is a strongly typed language, just now (.NET 4) is it starting to adopt a more dynamic behavior.

*Update: please take a look at this article: Mutable and immutable anonymous types, and keys,

Basically the reason why editing should NOT be allowed using anonymous types is that If you are using anonymous types as data-bound items and the fields were allowed to be editted the 'hashcodes' would change, so the new values and the object would no longer be discoverable within the table.

@Telerik -suggestion- could you please make the grid ReadOnly when it is bound to anonymous types?

Best Regards,
Emanuel Varga
0
Nikolay
Telerik team
answered on 14 Oct 2010, 03:16 PM
Hello guys,

Emanuel, thank you for your assistance.

RadGridView supports binding to IBindingList and INotifyPropertyChanged data objects which allows for automatic updates where necessary.

However, when you call the ToList() method on an IEnumerable object, this returns only a snapshot of the data. The change listeners are not available anymore, and you can't return the changes done in RadGridView back to the database automatically.

Microsoft DataGridView does not allow for adding a new row if it is bound to a list object. RadGridView, instead, throws an exception if one tries to add a new row. We may consider implementing the same behavior in one of our next versions. However, we currently do not plan to make RadGridView ReadOnly by default if it is bound to a list object.

Best wishes,
Nikolay
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
Emanuel Varga
Top achievements
Rank 1
answered on 14 Oct 2010, 03:26 PM
Hello Nikolay,

I'm not talking about a grid bound to a list of object, I'm talking about making the grid ReadOnly when it is bound to a list of Anonymous objects. Please take a look at Microsoft's DataGridView, and you will see that this also turns to ReadOnly when it is bound to a list of Anonymous objects.

There is a very big difference in binding to an IEnumerable data source and binding to a list of anonymous objects.

Please create a list of anonymous objects and bind it to the grid, now try to change the value of one cell and please tell me the result.

Best Regards,
Emanuel Varga
0
Julian Benkov
Telerik team
answered on 20 Oct 2010, 11:11 AM
Hi Emanuel,

Thank you for your suggestions and comments. We will make additional research for scenarios where Anonymous types are used and will consider extending RadGridView, making its behavior closer to the behavior of the standard DataGridView.

Greetings,

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
SachinC
Top achievements
Rank 1
Answers by
SachinC
Top achievements
Rank 1
Emanuel Varga
Top achievements
Rank 1
Paul Patterson
Top achievements
Rank 1
Nikolay
Telerik team
Julian Benkov
Telerik team
Share this question
or