Event to add column at the end

15 posts, 1 answers
  1. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 26 Oct 2010 Link to this post

    Hi,

    I don't see any event so I can get in and add a new column at the end of the AutoGeneratingColumns, something like AutoGeneratedColumns so I can add a column at the end by C# code

    The problem I have in AutoGeneratingColumns is that I can only add a column before the last one as I can know that it is generating the last one but it's still not generated yet and I can't set the DisplayIndex of my new column as being the last one

    The only working code I've been able to do is this :

    private void rgvApplications_DataLoaded(object sender, EventArgs e)
    {
        if (rgvApplications.Columns.FirstOrDefault<Telerik.Windows.Controls.GridViewColumn>(c => c.UniqueName == "Open") == null)
            Gear.TelerikAddColumn(rgvApplications, "Open", "Open", "Open", true, false, 120, Telerik.Windows.Controls.SortingState.None, false);
    }

    Any better ideas ?
  2. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 26 Oct 2010 Link to this post

    I'm also trying by C# code to add a column which is a button and of course this button have the Click event so I can perform action for the row in which the button is pressed

    Any code example somewhere ?
  3. DevCraft banner
  4. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 26 Oct 2010 Link to this post

    Hi Marc Roussel,

    The DataLoaded event of RadGridView is a proper way of adding additional columns.

    All the best,
    Veselin Vasilev
    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
  5. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 26 Oct 2010 Link to this post

    Hello Marc Roussel,

    You can use Commands as shown in this online demo.

    Best wishes,
    Veselin Vasilev
    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
  6. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 26 Oct 2010 Link to this post

    Hi,

    Thank you for the fast answer,
    I see the demo but I don't understand.

    The need is not to use the xaml at all just C# code
    and adding the column at the end.

    Right now I've been able to add my column with the DataLoaded event
    but Now I need to make it a button  What I see in the demo is to make the button already available in the grid by xaml and this puts the button at the first possition and of course I have AutoGeneratedColumns to true

    I want this to be all by C# code as I want to make myself a favour and add it to my library so I can easily add a button to any grid I will need in the future
  7. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 26 Oct 2010 Link to this post

    To date, I have this code which effectively add my button to the grid but the XamlReader.Load doesn't permit to wire event so I don't know how to wire the click event from the grid which have the button in the column.  Any good hint ?

    rgvApplications.Columns["Open"].CellTemplate = XamlReader.Load(@"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
                                                                     xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
                                                                     <Button x:Name='btOpenApplication' Content='Open' Cursor = 'Hand' />
                                                                     </DataTemplate>") as DataTemplate;
      
    DataTemplate dt = rgvApplications.Columns["Open"].CellTemplate as DataTemplate;
    DependencyObject doe = dt.LoadContent();
      
    btOpenApplication = doe as Button;
    btOpenApplication.Click += new RoutedEventHandler(btOpenApplication_Click);

    The current code is an unfortunate try to wire the event but even thought there's no error, it's not getting in the event as I presume because the LoadContent is like another button thant the one in the grid.
  8. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 26 Oct 2010 Link to this post

    Hi Marc Roussel,

    Please find attached a sample project. Hope it helps.

    Greetings,
    Veselin Vasilev
    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
  9. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 26 Oct 2010 Link to this post

    It almost helps except that it's not a Delete button for the row I need but a Open button that does something else in my code then deleteing a row.

    And I don'T see an way to wire the event in the code


    So how do I wiree event so the click of the button doesn'T delete the row but do something elsewhere like calling a method or showing something on the interface
  10. Answer
    Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 26 Oct 2010 Link to this post

    Hi Marc Roussel,

    You just need to remove the Command property (so the data is not deleted) and subscribe to the Click event of the button as you would do in any other case:

    button.Click += new RoutedEventHandler(button_Click);


    Sincerely yours,
    Veselin Vasilev
    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
  11. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 26 Oct 2010 Link to this post

    Ok but the problem is that what I need to perform can't be done in the MyButtonColumn class as it is not exposed there.
    I tried to make the button public from the MyButtonColumn and wire it outside but it's not faisable as the button isn't yet created when Adding it with the DataLoaded.  I don'T know if you see the picture here
  12. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 26 Oct 2010 Link to this post

    Ok I slightly modified the class like this and it works now

    public class MyButtonColumn : GridViewColumn
    {
        public RadButton button;
        private MainPage CurrentPage = null;
      
        public MyButtonColumn(MainPage Page)
        {
            CurrentPage = Page;
        }
        public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
        {
            button = cell.Content as RadButton;
      
            if (button == null)
            {
                button = new RadButton();
                button.Content = "Open";
                button.Click += new RoutedEventHandler(button_Click);
            }
      
            button.CommandParameter = dataItem;
      
            return button;
        }
      
        void button_Click(object sender, RoutedEventArgs e)
        {
            CurrentPage.IsNewApplication = false;
            CurrentPage.gridApplication.DataContext = CurrentPage.SelectedApplication;
            VisualStateManager.GoToState(CurrentPage, "ShowApplicationDetail", true);
        }
    }
  13. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 26 Oct 2010 Link to this post

    Thank you for this...
    Just one little issue.

    When I click the button and the action is perform, how do I get the object of the row ?
    I tried with _Button.CommandParameter, the DataItem isn't the row for the which I clicked he button

    Let me explain so it is clear,

    when I click a button, the object under the row for which I click the button I need it as this becomes the DataContext of what I have to show the the user.  But since the row isn't selected when I click the button, I can't use the Grid.SelectedItem so how do I get my object out of it
  14. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 26 Oct 2010 Link to this post

    Ok forget it, I find it it's ((RadButton)sender).CommandParameter
  15. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 26 Oct 2010 Link to this post

    public class MyButtonColumn : GridViewColumn 
        public RadButton button; 
        private MainPage CurrentPage = null
        
        public MyButtonColumn(MainPage Page) 
        
            CurrentPage = Page; 
        
        public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem) 
        
            button = cell.Content as RadButton; 
        
            if (button == null
            
                button = new RadButton(); 
                button.Content = "Open"
                button.Click += new RoutedEventHandler(button_Click); 
            
        
            button.CommandParameter = dataItem; 
        
            return button; 
        
        
        void button_Click(object sender, RoutedEventArgs e) 
        
            CurrentPage.IsNewApplication = false
            CurrentPage.gridApplication.DataContext = ((RadButton)sender).CommandParameter; 
            VisualStateManager.GoToState(CurrentPage, "ShowApplicationDetail", true); 
        
    }

    I posted the working code in case anyone else would need it :
  16. Marc Roussel
    Marc Roussel avatar
    429 posts
    Member since:
    Dec 2009

    Posted 26 Oct 2010 Link to this post

    I'm happy that it works but it's definitely not a good approach as for instence if I have 3 columns with 3 different actions to perform, I'm forced to rely on a parameter passed to the class to determine what action needs to be performed like this :

    private void _button_Click(object sender, RoutedEventArgs e)
    {
        if (_Caption == "Open")
        {
            _CurrentPage.IsNewApplication = false;
            _CurrentPage.gridApplication.DataContext = ((RadButton)sender).CommandParameter;
            VisualStateManager.GoToState(_CurrentPage, "ShowApplicationDetail", true);
        }
      
        if (_Caption == "Start")
        {
            //TODO:
        }
      
        if (_Caption == "Stop")
        {
            //TODO:
        }
    }
Back to Top
DevCraft banner