Hello,
I have a telerik radGridView that itemsSource binding to my list observableCollection<Class1>
i have a button that on her click event i i add a new item to my list, i want focus the new item first cell,
so i want do it with the event AddingNewDataItem but it not camming to this event.
heer is my xaml code:
<Grid>
<telerik:RadGridView x:Name="PricingGrid" ItemsSource="{Binding}"
CanUserInsertRows="True" ShowInsertRow="True"
AddingNewDataItem="PricingGrid_AddingNewDataItem"
RowEditEnded="PricingGrid_RowEditEnded" >
</telerik:RadGridView>
<Button Content="Button" HorizontalAlignment="Left" Height="61" Margin="269,237,0,0" VerticalAlignment="Top" Width="176" Click="Button_Click"/>
</Grid>
heer is my C# code:
public partial class MainWindow : Window
{
public CollectionViewSource Source { get; set; }
public ObservableCollection<MyClass> list = new ObservableCollection<MyClass>();
public MainWindow()
{
InitializeComponent();
this.Source = new CollectionViewSource();
//var list = new ObservableCollection<MyClass>();
for (int i = 0; i < 10; i++)
{
var item = new MyClass { MyProp2 = i, MyProperty = i * 10 };
list.Add(item);
}
Source.Source = list;
this.DataContext = Source.View;
}
private void PricingGrid_RowEditEnded(object sender, Telerik.Windows.Controls.GridViewRowEditEndedEventArgs e)
{
if (e.EditOperationType == Telerik.Windows.Controls.GridView.GridViewEditOperationType.Insert)
{
this.DataContext = Source.View.OfType<MyClass>().OrderBy(c => c.MyProperty);
Source.Source = this.DataContext;
PricingGrid.SelectedItem = e.NewData;
PricingGrid.ScrollIntoView(e.NewData);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
list.Add(new MyClass { MyProp2 = 4, MyProperty = 40 });
}
private void PricingGrid_AddingNewDataItem(object sender, Telerik.Windows.Controls.GridView.GridViewAddingNewEventArgs e)
{
}
}
public class MyClass
{
public int MyProperty { get; set; }
public int MyProp2 { get; set; }
}
thank for help!
miri.
7 Answers, 1 is accepted
Please take a look at the Adding New Entries article of our documentation.
The reason the AddingNewDataItem is not fired is because it is raised by BeginInsert() which you have not used in your implementation.
So you simply have to replace
list.Add(
new
MyClass { MyProp2 = 4, MyProperty = 40 });
this
.PricingGrid.BeginInsert();
e.NewObject =
new
MyClass { MyProp2 = 4, MyProperty = 40 };
Regards,
Dilyan Traykov
Telerik
thank,
but it not exactly what i want.
i do want add from view to model.
and i want add the row to spesifial index,
so i change and put e.cancel=true,
but now i cannot doing the focus :
my code :
public partial class MainWindow : Window
{
public CollectionViewSource Source { get; set; }
public ObservableCollection<MyClass> list = new ObservableCollection<MyClass>();
public MainWindow()
{
InitializeComponent();
this.Source = new CollectionViewSource();
//var list = new ObservableCollection<MyClass>();
for (int i = 0; i < 10; i++)
{
var item = new MyClass { MyProp2 = i, MyProperty = i * 10 };
list.Add(item);
}
Source.Source = list;
this.DataContext = Source.View;
}
private void PricingGrid_RowEditEnded(object sender, Telerik.Windows.Controls.GridViewRowEditEndedEventArgs e)
{
if (e.EditOperationType == Telerik.Windows.Controls.GridView.GridViewEditOperationType.Insert)
{
this.DataContext = Source.View.OfType<MyClass>().OrderBy(c => c.MyProperty);
Source.Source = this.DataContext;
PricingGrid.SelectedItem = e.NewData;
PricingGrid.ScrollIntoView(e.NewData);
}
}
MyClass dd = new MyClass { MyProp2 = 4, MyProperty = 40 };
int i = 0;
private void Button_Click(object sender, RoutedEventArgs e)
{
this.PricingGrid.BeginInsert();
dd = new MyClass { MyProp2 = 4+i, MyProperty = 40+i };
i++;
list.Add(dd);
}
private void PricingGrid_AddingNewDataItem(object sender, Telerik.Windows.Controls.GridView.GridViewAddingNewEventArgs e)
{
e.NewObject = dd;
e.Cancel = true;
}
}
I'd like to ask whether you need the rows to be sorted after you've added the new one, or does it have to appear somewhere in-between the other rows? Also what specific index are you referring to?
When we have that information we can look for a better solution.
Regards,
Dilyan Traykov
Telerik
more detials:
1. i have insert the new item to specifila index, and i know only on the cs which index it, the index to add change Depending on the parameters set out in CS.
2. i dont have to sort the radGridView After adding.
2. my data is very difficult, i have a lot of field, that the user cannot edit but thay are display, this items dont have a fixed value, i set her values start over each time, so i must do first the item on my data and just after this show it to the user
As this seems to be a very specific scenario, I'm afraid there's no straightforward way to achieve this.
I've prepared a sample project based on your first request to show you how it could be done.
Of course, you would have to change the algorithm for finding the insertion index, based on your logic.
However, the scenario you've described in your last post is very different from the one we started with. It implies that some if not all of the columns are disabled, which brings the question of how you will add the items. If you will add them through the code itself, you can easily choose at which index to add them with the Insert() method of the collection.
Please have a look at the sample project and let me know if it solves your problem. If that is not the case, we will need a better explanation of the situation in order to investigate further.
Regards,
Dilyan Traykov
Telerik
Thank for you Quick answer.
i think that it can be good, but i have a problem.
in your project it work good,
but at me the line "PricingGrid.BeginEdit()" alweys focus and edit the firs row.
i m debugging your application and i saw that also in your application the metodes "PricingGrid.BeginEdit()" return false.
can you please help me on this problem?
You can find more information about BeginEdit() in this article. BeginEdit's return type is void, thus it does not return anything. It's just tells RadGridView to start editing the selected item. So you need to make sure that you successfully select the added row. In my project this is accomplished by the following lines:
PricingGrid.SelectedItem = e.NewObject;
PricingGrid.BeginEdit();
Please make sure you're correctly selecting the newly-inserted item and if you still encounter problems please provide some sample code to help us investigate further.
Regards,
Dilyan Traykov
Telerik