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
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.
Maya
the Telerik team
Thanks.
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
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
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.
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?
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????