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

Remove selected Items from GridView with itemsSource set to DataTable

2 Answers 655 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
William
Top achievements
Rank 1
William asked on 24 Nov 2010, 06:00 AM
I have a radtabstrip that contains tabs with one gridView per tab. The tabs are added and removed dynamically. 
I am trying to delete selected items from a GridView in the current tab that has an ItemsSource set to a DataTable. What is the preferred way to remove the selected items?

Here is what I have so far...This is not all the code
public ObservableCollection<DataTable> _data { get; set; }



This is how I assign the ItemsSource:
foreach (DataTable table in _data)
{
    RadTabItem ttab = CreateTab(table.DataName);
    (ttab.Content as RadGridView).ItemsSource = table;
    
    TabControl.Items.Add(ttab);
    // ttab = null;
}


I have a button with the click event contains something like this:
foreach (var row in ((TabControl.SelectedItem as RadTabItem).Content as RadGridView).SelectedItems)
           {
               MessageBox.Show((row.GetType().ToString()));
               ((TabControl.SelectedItem as RadTabItem).Content as RadGridView).Items.Remove(row as DataRow);
           }

Thought about trying to modify the _data variable but not sure how to associate the selecteditems back to the correct table and rows in _data.

2 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 26 Nov 2010, 07:44 AM
Hello,

 You can remove the grid selected items directly from the UI if you turn on multiple selection and the grid is not read only. You can check this demo for more info. 

Greetings,
Vlad
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
William
Top achievements
Rank 1
answered on 27 Nov 2010, 03:03 AM
That looks interesting, but I am still not able to get that to work. I have a user control that contains a radtabstrip that is dynamically generated from a   public ObservableCollection<DataTable> _data { get; set; }

Each DataTable in the observableCollection beomes a new tab in the TabControl with the content of a Dataview.

I build the radviews in code and databind them like so:
private RadTabItem CreateTab(string name)
{
    RadTabItem tab = new RadTabItem();
     
    TextBlock htext = new TextBlock() { Text = name };
    htext.Margin = new Thickness(0, 0, 3, 0);
    Button hbtn = new Button();
    hbtn.Content = "X";
    hbtn.VerticalContentAlignment = System.Windows.VerticalAlignment.Top;
    hbtn.Height = 18;
    hbtn.Tag = tab;
    hbtn.BorderThickness = new Thickness(0);
    hbtn.Click += new RoutedEventHandler(hbtn_Click);
    StackPanel layout = new StackPanel();
    layout.Orientation = Orientation.Horizontal;
 
    layout.Children.Add(htext);
    layout.Children.Add(hbtn);
    tab.Header = layout;
    RadGridView grid = new RadGridView();
    grid.CanUserDeleteRows = true;
    grid.CanUserFreezeColumns = false;
    grid.CanUserInsertRows = false;
    grid.CanUserReorderColumns = true;
    grid.CanUserResizeColumns = true;
    grid.CanUserSelect = true;
    grid.IsReadOnly = false;
    grid.SelectionMode = System.Windows.Controls.SelectionMode.Multiple;
    grid.EditTriggers = GridViewEditTriggers.None;
    grid.MaxHeight = 200;
    GridViewSelectColumn sel = new GridViewSelectColumn();
    grid.Columns.Add(sel);
    tab.Content = grid;
    return tab;
}

I changed my button to look like this:
<telerik:RadButton x:Name="Btndelsel" Content="Delete Selected" Command="telerikGrid:RadGridViewCommands.Delete" Height="20" Margin="4,0,4,0" Width="110" HorizontalAlignment="Left" />

I have declared my  RadTabControl as :
<telerik:RadTabControl x:Name="TabControl" Grid.Row="1" Margin="0,0,0,0" SelectionChanged="TabControl_SelectionChanged">
     
</telerik:RadTabControl>


With the TabControl_SelectionChanged event looking like this:
private void TabControl_SelectionChanged(object sender, RoutedEventArgs e)
{
    if (TabControl.Items.Count == 0)
    {
        TxtRowCount.Text = "";
    }
    else
    {
        TxtRowCount.Text = ((TabControl.SelectedItem as RadTabItem).Content as RadGridView).Items.Count.ToString();
        Btndelsel.CommandTarget = ((TabControl.SelectedItem as RadTabItem).Content as RadGridView);
        
    }
}

So I end up with a bunch of tabs that contain gridviews. Besides needing to delete individual rows, I also need to preform operations on the different rows based on the data that the rows contain via other buttons.

When I do something like:
MessageBox.Show(((TabControl.SelectedItem as RadTabItem).Content as RadGridView).SelectedItem.GetType().ToString() );

I get: 
DynamicObjectBuilder_<guid>


I have tried casting it to DataTable, DataRow,  ObservableCollection<DataTable>,  ObservableCollection<DataRow>,  ObservableCollection<Dynamic>
I can't seem to get access to the values contained in the selected rows.

Thanks!

Tags
GridView
Asked by
William
Top achievements
Rank 1
Answers by
Vlad
Telerik team
William
Top achievements
Rank 1
Share this question
or