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

Manually adding item in AddingNewDataItem not working

8 Answers 311 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Datafyer
Top achievements
Rank 1
Veteran
Datafyer asked on 26 Apr 2011, 10:33 PM
I have been using the WPF version of Telerik for a while and I truly think it is a wonderful product. Recently I began exploring the Silverlight side and am running into a few issues.

I am exploring the possibility of having the user click on the new row button in the RadGridView, display a dialog, and then create a new row based on the information in the dialog. I created some very simple code which should do this, but it is not working. The new rows are never displayed in the grid. This is probably my lack of silverlight understand. The code below should add the value 10 to the grid, but it doesn't. However, if the same line is put outside of the closed inner event then it works fine.

namespace TestSilverlight
{
  public partial class MainPage : UserControl
  {
    private ObservableCollection<int> List = new ObservableCollection<int>();
  
    public MainPage()
    {
      InitializeComponent();
  
      GridView.ItemsSource = List;
      List.AddRange(new int[] { 1, 2, 3, 4, 5 });
    }
  
    private void GridView_AddingNewDataItem(object sender, Telerik.Windows.Controls.GridView.GridViewAddingNewEventArgs e)
    {
      RadWindow NewWindow = new RadWindow();
      NewWindow.Closed += delegate
      {
        //Add new row based on dialog choices
        List.Add(10);
      };
  
      NewWindow.ShowDialog();
    }
  }
}

8 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 27 Apr 2011, 07:47 AM
Hi Patrick,

I am sending you a sample project illustrating how you may achieve a similar scenario to yours. Please take a look at it and notify me in case it does not correspond to your exact requirements.
 

Best wishes,
Maya
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
Datafyer
Top achievements
Rank 1
Veteran
answered on 28 Apr 2011, 07:38 PM
I ran the code and it worked which is nice. It is however doing the same thing I was doing, which is adding to the list directly. I have no idea why my sample was not working.

Thanks.
0
Yavor Georgiev
Telerik team
answered on 28 Apr 2011, 08:20 PM
Hi Patrick,

 The AddingNewDataItem event fires only when the RadGridView is about to create a new data item - e.g. a user clicked on the Insert Row or pressed the Insert key on their keyboard.

 Imagine that for some reason the type of the items in a RadGridView doesn't have a default parameterless constructor, or you just need to do some extra initialization before the item is added to the collection - that is the intended usage for the event. In fact, as the DataItemCollection that the RadGridView relies upon to handle sorting, filtering, paging, etc. implements IEditableCollectionView, AddingNewDataItem is part of the AddNew()/CommitNew() cycle. Thus when you add an item to your collection through your AddingNewDataItem handler, there is some internal CollectionView logic that is executed prior to the item actually landing in the source collection.

Greetings,
Yavor Georgiev
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
mike okon
Top achievements
Rank 1
answered on 06 Feb 2013, 11:56 AM
How can I use this event to prepopulate a cell in the new row that is created on gridview. How can I reference a particular cell on the new row and give it a default value.

In the example the player position is set to GK. however this is in the popup window. Im looking to do the same but just not using the popup window and rather use the gridview. I still need to enter the default value before the user sees the new row.

Is this possible? ALSO  I can not see the code where the player position is pre filled to GK. where is the logic that does this task?

regards
mike
0
Maya
Telerik team
answered on 06 Feb 2013, 12:08 PM
Hello Mike,

I would definitely recommend you to run through our documentation and specifically to this article. As you can see you can handle AddingNewDataItem event and perform the logic you want inside. For example:

e.NewObject = new Employee() {Name = "NewName"};
   
Still, you should keep in mind that you have to work with the data items and its properties and not with the visual elements - like in this case cells and their values.
Considering your last question about the Position property, as you probably know, the default value for an enum is 0 and if such is not specifically defined, the first available value is selected.

All the best,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
mike okon
Top achievements
Rank 1
answered on 06 Feb 2013, 12:39 PM
Thank you Maya. You example solved my issue until you told me off for doing it. I am now suspect it is the best way of doing it. How do I work with the data items and its properties?

What you say is probably more correct for my requirements. What I need to do is pre fill the UserId Guid.  Now the code I use to get it is on the server side Silverlight Ria Services Business app project.
public static object GetCurrentUserId()
        {
                        return (Guid)Membership.GetUser().ProviderUserKey;
        }


However this is on the server and I need to ref the GetCurrentUserId back into the userId column for the new row. As you say Im filling the cell value but what is the preferred way to update the DB with the required value. I am using OpenAccess and Domaindatasource. Ive attached a pic of what is happening when a new row is created. Is there a way to program the DomainDataSource to be aware of the Currently logged In UserId?

0
mike okon
Top achievements
Rank 1
answered on 06 Feb 2013, 01:49 PM
I have managed to Pass the UserId as follows

Edit the AuthenticationService.cs as follows
[EnableClientAccess]
   public class AuthenticationService : AuthenticationBase<User>
    
   {
       protected override User GetAuthenticatedUser(System.Security.Principal.IPrincipal principal)
       {
           User user = base.GetAuthenticatedUser(principal);
           var memUser = Membership.GetUser(principal.Identity.Name, true);
           user.UserID = (Guid)memUser.ProviderUserKey;
 
           return user;
       }
   }
       public partial class User : UserBase
       {
           [ProfileUsage(IsExcluded = true)]
           public Guid UserID { get; set; }
       }

After that I could pass in the UserId into the cell as per your example?

private void radGridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            Guid userID = WebContext.Current.User.UserID;
            e.NewObject = new CourseInfo() { UserId = userID };
      }

The result is the current logged in user is placed in the cell value see pic.

Now For my question. Does this go against Teleriks way of doing this. (by changing the cell value) How should I change the data item and its property and not its element????


0
Rossen Hristov
Top achievements
Rank 2
answered on 06 Feb 2013, 05:23 PM
That is the right way to do it.
Tags
GridView
Asked by
Datafyer
Top achievements
Rank 1
Veteran
Answers by
Maya
Telerik team
Datafyer
Top achievements
Rank 1
Veteran
Yavor Georgiev
Telerik team
mike okon
Top achievements
Rank 1
Rossen Hristov
Top achievements
Rank 2
Share this question
or