I don't found any example about deleting operation. How i can remove an item row from gridview itemSource?
I found Remove commands, but how i can get the type object to remove?
Example :
model :
public
class
City
{
public
City(
string
name,
int
population)
{
this
.Name = name;
this
.Population = population;
}
public
string
Name {
get
;
set
; }
public
int
Population {
get
;
set
; }
}
I expected that i can pass myGrid.SelectedItem to Remove method, but dosen't work because Remove wants another object type. I'm sorry but is first time that i use Telerik object, and i don't found an example about delete on Xamarin.Forms Data Grid doumentation or in SDK example.
13 Answers, 1 is accepted
I've checked the Remove method of the ItemsSource and it works properly at my side, just the SelectedItem should be cast to the expected object (of type City in the concrete case). Here is a quick snippet you could try:
var selectedItem = gridTest.SelectedItem
as
City;
if
(selectedItem !=
null
)
{
(gridTest.ItemsSource
as
ObservableCollection<City>).Remove(selectedItem);
}
I hope this would be helpful.
Regards,
Yana
Progress Telerik
This will also depend on what you're using ofr an items source. If you're getting null for casting to ObservableCollection<City>, then the items source is not an ObservableCollection<City>.
My recommendation is to change the bound source to use ObservableCollection, after this change, Yana's recommendation will work. In addition to getting access to IList based features, you also get CollectionChanged notifications and the DataGrid will be updated whenever new items are added/removed elsewhere.
Alternative:
if you cannot change the source type, then you'll want to cast the type appropriately:
(gridTest.ItemsSource
as
YourIListBasedType<City>).Remove()
For example, if the DataGrid is using:
// The items source is of type List<City>
gridTest.ItemsSource =
new
List<City>
// then you can do
(gridTest.ItemsSource
as
List<City>).Remove()
Ultimately, the Remove() method is only available for objects based on IList (List<T>, Collection<T> etc). If the source type is of DataTable or other non-List types, you can use that type's item removal approach.
Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Maybe i found the problem .. after searching result data for itemSoruce i order the result :
gridTotal.ItemsSource = searchResults.OrderByDescending(o => o.IssuingDate); //issuing date is a dateTime property in my model object.
if i remove OrderBy the remove code work fine..
This is the xaml of grid :
<
telerikDataGrid:RadDataGrid
x:Name
=
"gridTotal"
Margin
=
"60,0,60,0"
SelectionMode
=
"Single"
SelectionUnit
=
"Row"
UserFilterMode
=
"Disabled"
UserGroupMode
=
"Disabled"
UserSortMode
=
"None"
AutoGenerateColumns
=
"False"
ItemsSource
=
"{Binding TransactionResponseModelForGrid}"
Grid.Row
=
"10"
Grid.Column
=
"0"
Grid.ColumnSpan
=
"4"
Grid.RowSpan
=
"14"
SelectionChanged
=
"grid_SelctionChanged"
>
<
telerikDataGrid:RadDataGrid.SortDescriptors
>
<
common:PropertySortDescriptor
PropertyName
=
"IssuingDate"
SortOrder
=
"Descending"
/>
</
telerikDataGrid:RadDataGrid.SortDescriptors
>
<
telerikDataGrid:RadDataGrid.SelectionStyle
>
<
telerikDataGrid:DataGridBorderStyle
BackgroundColor
=
"LightGray"
BorderColor
=
"Black"
BorderThickness
=
"1"
/>
</
telerikDataGrid:RadDataGrid.SelectionStyle
>
<
telerikDataGrid:RadDataGrid.Columns
>
<
telerikDataGrid:DataGridTextColumn
PropertyName
=
"Id"
HeaderText
=
"TRANSACTION ID"
Width
=
"150"
SizeMode
=
"Fixed"
>
<
telerikDataGrid:DataGridTextColumn.CellDecorationStyle
>
<
telerikDataGrid:DataGridBorderStyle
BorderThickness
=
"1, 0.5, 0.5, 0.5"
BorderColor
=
"White"
/>
</
telerikDataGrid:DataGridTextColumn.CellDecorationStyle
>
<
telerikDataGrid:DataGridTextColumn.HeaderStyle
>
<
telerikDataGrid:DataGridColumnHeaderStyle
OptionsButtonTextColor
=
"Transparent"
BackgroundColor
=
"Black"
TextColor
=
"White"
HorizontalTextAlignment
=
"Center"
TextFontSize
=
"10"
BorderColor
=
"#D9D9D9"
BorderThickness
=
"1"
/>
</
telerikDataGrid:DataGridTextColumn.HeaderStyle
>
<
telerikDataGrid:DataGridTextColumn.CellContentStyle
>
<
telerikDataGrid:DataGridTextCellStyle
TextColor
=
"White"
HorizontalTextAlignment
=
"Center"
FontSize
=
"12"
/>
</
telerikDataGrid:DataGridTextColumn.CellContentStyle
>
</
telerikDataGrid:DataGridTextColumn
>
<
telerikDataGrid:DataGridDateColumn
PropertyName
=
"IssuingDate"
HeaderText
=
"ISSUING DATE"
Width
=
"115"
SizeMode
=
"Fixed"
CellContentFormat
=
"{}{0:dd/MM/yyyy}"
>
<
telerikDataGrid:DataGridTextColumn.CellDecorationStyle
>
<
telerikDataGrid:DataGridBorderStyle
BorderThickness
=
"1, 0.5, 0.5, 0.5"
BorderColor
=
"White"
/>
</
telerikDataGrid:DataGridTextColumn.CellDecorationStyle
>
<
telerikDataGrid:DataGridTextColumn.HeaderStyle
>
<
telerikDataGrid:DataGridColumnHeaderStyle
OptionsButtonTextColor
=
"Transparent"
BackgroundColor
=
"Black"
TextColor
=
"White"
HorizontalTextAlignment
=
"Center"
TextFontSize
=
"10"
BorderColor
=
"#D9D9D9"
BorderThickness
=
"1"
/>
</
telerikDataGrid:DataGridTextColumn.HeaderStyle
>
<
telerikDataGrid:DataGridDateColumn.CellContentStyle
>
<
telerikDataGrid:DataGridTextCellStyle
TextColor
=
"White"
FontSize
=
"12"
HorizontalTextAlignment
=
"Center"
>
</
telerikDataGrid:DataGridTextCellStyle
>
</
telerikDataGrid:DataGridDateColumn.CellContentStyle
>
</
telerikDataGrid:DataGridDateColumn
>
<
telerikDataGrid:DataGridTextColumn
PropertyName
=
"MemberFullName"
HeaderText
=
"FULL NAME"
Width
=
"140"
SizeMode
=
"Fixed"
>
<
telerikDataGrid:DataGridTextColumn.CellDecorationStyle
>
<
telerikDataGrid:DataGridBorderStyle
BorderThickness
=
"1, 0.5, 0.5, 0.5"
BorderColor
=
"White"
/>
</
telerikDataGrid:DataGridTextColumn.CellDecorationStyle
>
<
telerikDataGrid:DataGridTextColumn.HeaderStyle
>
<
telerikDataGrid:DataGridColumnHeaderStyle
OptionsButtonTextColor
=
"Transparent"
BackgroundColor
=
"Black"
TextColor
=
"White"
HorizontalTextAlignment
=
"Center"
TextFontSize
=
"10"
BorderColor
=
"#D9D9D9"
BorderThickness
=
"1"
/>
</
telerikDataGrid:DataGridTextColumn.HeaderStyle
>
<
telerikDataGrid:DataGridTextColumn.CellContentStyle
>
<
telerikDataGrid:DataGridTextCellStyle
TextColor
=
"White"
FontSize
=
"12"
/>
</
telerikDataGrid:DataGridTextColumn.CellContentStyle
>
</
telerikDataGrid:DataGridTextColumn
>
<
telerikDataGrid:DataGridDateColumn
PropertyName
=
"MemberBirthday"
HeaderText
=
"DAY OF BIRTH"
Width
=
"110"
SizeMode
=
"Fixed"
CellContentFormat
=
"{}{0:dd/MM/yyyy}"
>
<
telerikDataGrid:DataGridTextColumn.CellDecorationStyle
>
<
telerikDataGrid:DataGridBorderStyle
BorderThickness
=
"1, 0.5, 0.5, 0.5"
BorderColor
=
"White"
/>
</
telerikDataGrid:DataGridTextColumn.CellDecorationStyle
>
<
telerikDataGrid:DataGridTextColumn.HeaderStyle
>
<
telerikDataGrid:DataGridColumnHeaderStyle
OptionsButtonTextColor
=
"Transparent"
BackgroundColor
=
"Black"
TextColor
=
"White"
TextFontSize
=
"10"
HorizontalTextAlignment
=
"Center"
BorderColor
=
"#D9D9D9"
BorderThickness
=
"1"
/>
</
telerikDataGrid:DataGridTextColumn.HeaderStyle
>
<
telerikDataGrid:DataGridDateColumn.CellContentStyle
>
<
telerikDataGrid:DataGridTextCellStyle
TextColor
=
"White"
FontSize
=
"12"
HorizontalTextAlignment
=
"Center"
>
</
telerikDataGrid:DataGridTextCellStyle
>
</
telerikDataGrid:DataGridDateColumn.CellContentStyle
>
</
telerikDataGrid:DataGridDateColumn
>
<
telerikDataGrid:DataGridNumericalColumn
PropertyName
=
"GrossAmount"
HeaderText
=
"GROSS AMOUNT"
Width
=
"100"
SizeMode
=
"Fixed"
CellContentFormat
=
"{}{0:N}"
>
<
telerikDataGrid:DataGridTextColumn.CellDecorationStyle
>
<
telerikDataGrid:DataGridBorderStyle
BorderThickness
=
"1, 0.5, 0.5, 0.5"
BorderColor
=
"White"
/>
</
telerikDataGrid:DataGridTextColumn.CellDecorationStyle
>
<
telerikDataGrid:DataGridTextColumn.HeaderStyle
>
<
telerikDataGrid:DataGridColumnHeaderStyle
OptionsButtonTextColor
=
"Transparent"
BackgroundColor
=
"Black"
TextColor
=
"White"
HorizontalTextAlignment
=
"Center"
TextFontSize
=
"10"
BorderColor
=
"#D9D9D9"
BorderThickness
=
"1"
/>
</
telerikDataGrid:DataGridTextColumn.HeaderStyle
>
<
telerikDataGrid:DataGridDateColumn.CellContentStyle
>
<
telerikDataGrid:DataGridTextCellStyle
TextColor
=
"White"
FontSize
=
"12"
HorizontalTextAlignment
=
"End"
>
</
telerikDataGrid:DataGridTextCellStyle
>
</
telerikDataGrid:DataGridDateColumn.CellContentStyle
>
</
telerikDataGrid:DataGridNumericalColumn
>
<
telerikDataGrid:DataGridTextColumn
PropertyName
=
"Status"
HeaderText
=
"STATE"
Width
=
"60"
SizeMode
=
"Fixed"
>
<
telerikDataGrid:DataGridTextColumn.CellDecorationStyle
>
<
telerikDataGrid:DataGridBorderStyle
BorderThickness
=
"1, 0.5, 0.5, 0.5"
BorderColor
=
"White"
/>
</
telerikDataGrid:DataGridTextColumn.CellDecorationStyle
>
<
telerikDataGrid:DataGridTextColumn.HeaderStyle
>
<
telerikDataGrid:DataGridColumnHeaderStyle
OptionsButtonTextColor
=
"Transparent"
BackgroundColor
=
"Black"
TextColor
=
"White"
HorizontalTextAlignment
=
"Center"
TextFontSize
=
"10"
BorderColor
=
"#D9D9D9"
BorderThickness
=
"1"
/>
</
telerikDataGrid:DataGridTextColumn.HeaderStyle
>
<
telerikDataGrid:DataGridTextColumn.CellContentStyle
>
<
telerikDataGrid:DataGridTextCellStyle
TextColor
=
"White"
FontSize
=
"12"
/>
</
telerikDataGrid:DataGridTextColumn.CellContentStyle
>
</
telerikDataGrid:DataGridTextColumn
>
</
telerikDataGrid:RadDataGrid.Columns
>
</
telerikDataGrid:RadDataGrid
>
Correct, that's the exact reason you cannot cast DataGrid.ItemsSource to ObservableCollection<T>. The result of a LINQ query is of type Enumerable, in your specific case, it returns an IOrderedEnumerable, which does not have a Remove method by default.
IOrderedEnumerable<YourModel> linqResult = searchResults.OrderByDescending(o => o.IssuingDate)
You could add one using LINQ extensions, but let me give you a simpler solution:
gridTotal.ItemsSource = searchResults.OrderByDescending(o => o.IssuingDate).ToList();
Then you can cast the ItemsSource to type List and use the Remove method
(gridTotal.ItemsSource
as
List<YourModel>).Remove(selectedItem);
Side Note
You don't need to use LINQ to sort searchResults before assigning the ItemsSource. It looks like you already have a PropertySortDescriptor assigned for IssuingDate property.
<
telerikDataGrid:RadDataGrid.SortDescriptors
>
<
common:PropertySortDescriptor
PropertyName
=
"IssuingDate"
SortOrder
=
"Descending"
/>
</
telerikDataGrid:RadDataGrid.SortDescriptors
>
This means the code is doubling the amount of work it has to do (sorting once on searchResult and again in the DataGrid).
I recommend just leaving the DataGrid one in place and remove the LINQ code, because the DataGrid lets the user see that the data is sorted in the UI (via the Sort indicator in the column header).
Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
When using multiple operators in sequence, make sure you clear the previous ones to ensure that only the operators you want are being applied. For example:
// Ensure the current descriptors are cleared before adding the ones you want to use
dataGrid.SortDescriptors.Clear();
dataGrid.FilterDescriptors.Clear();
dataGrid.GroupDescriptors.Clear();
// Add the descriptors in the order you want them
dataGrid.SortDescriptors.Add(
new
PropertySortDescriptor { PropertyName =
"Property1"
, SortOrder = SortOrder.Ascending});
dataGrid.SortDescriptors.Add(
new
PropertySortDescriptor { PropertyName =
"Property2"
, SortOrder = SortOrder.Descending});
As far as your specific setup goes, don't sort the original data first, just set the ItemsSource and then add the descriptors you want to use.
Further Investigation
I have tested several configurations of the DataGrid's Sort/Filter/Grouping operators and they appear to working as expected. However, this may not be the case for your specific data or configuration.
Therefore, if you continue to have issues with sorting/filtering, we will need to investigate directly, please take the following steps so that we can help further:
1 - Open a Support ticket: https://www.telerik.com/account/support-tickets/contact-support-team
2 - Attach screenshots of the before and after, and explain why it doesn't look like you expect it to
3 - Attach all the code for your DataGrid implementation so that we can reproduce it.
Important: Since this scenario is very specific to your data, we'll need a way to view your data in the RadDataGrid when reproducing it. An easy way to do this is to just create a temporary method that generates sample values for the items source.
For example:
private
ObservableCollection<MyModel> GenerateTestData()
{
return
new
ObservableCollection<MyModel>()
{
new
MyModel {Property1 =
"example value 1"
, Property2 =
"example value"
},
new
MyModel {Property1 =
"example value 2"
, Property2 =
"example value"
},
new
MyModel {Property1 =
"example value 3"
, Property2 =
"example value"
},
new
MyModel {Property1 =
"example value 4"
, Property2 =
"example value"
},
new
MyModel {Property1 =
"example value 5"
, Property2 =
"example value"
},
};
}
then we can use something like the following to test the issue:
private
void
TestDescriptors()
{
// Sample data for Telerik Support
dataGrid.ItemsSource = GenerateTestData();
dataGrid.SortDescriptors.Clear();
dataGrid.SortDescriptors.Add(
new
PropertySortDescriptor { PropertyName =
"Property1"
, SortOrder = SortOrder.Ascending });
dataGrid.SortDescriptors.Add(
new
PropertySortDescriptor { PropertyName =
"Property2"
, SortOrder = SortOrder.Descending });
}
We will immediately see your Support Ticket come in and either I or my colleagues will dig deeper to find out what's going on in that scenario.
Forum Error
If you see the forum posting error again, please grab a screenshot and let me know. I'll report it directly to the engineers responsible for the portal.
Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
How can i do the same for Xamarin Forms MvvmCross ?
OnItemSelect => Alert "Are you sure you want t delete?" => Yes {Delete row }
You can treat this situation in the same way you would with using a ListView. The fundamental takeaway is that the logic never removes a DataGrid row. Instead, it removes an item from the data source itself.
For example:
MyDataSource.Remove(itemToRemove);
This is why we recommend using an ObservableCollection<T> for ItemsSource because when the collection changes anything bound to it will get a CollectionChanged notification and update accordingly.
To answer your question directly, just access the ObservableCollection and remove the item from the collection in your Alert's logic. For example, if the data source's name is MyItems:
<
telerik:RadDataGrid
ItemsSource
=
"{Binding MyItems}"
/>
You would remove an item like this:
OnItemSelect => Alert
"Are you sure you want to delete?"
=> Yes { MyItems.Remove(item) }
Note: The DataGrid relies on the data source for notifications on item changes. If you're not using ObservableCollection or a collection that notifies consumers, then you may need to call PropertyChanged notification on the collection.
I hope this is helpful. If you would like to see CRUD example, take a look at the official CRUD demo here.
Regards,
Lance | Technical Support Engineer, Principal
Progress Telerik