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

Event to add column at the end

14 Answers 118 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Marc Roussel
Top achievements
Rank 2
Marc Roussel asked on 26 Oct 2010, 11:21 AM
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 ?

14 Answers, 1 is accepted

Sort by
0
Marc Roussel
Top achievements
Rank 2
answered on 26 Oct 2010, 11:33 AM
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 ?
0
Veselin Vasilev
Telerik team
answered on 26 Oct 2010, 11:34 AM
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
0
Veselin Vasilev
Telerik team
answered on 26 Oct 2010, 11:36 AM
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
0
Marc Roussel
Top achievements
Rank 2
answered on 26 Oct 2010, 12:05 PM
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
0
Marc Roussel
Top achievements
Rank 2
answered on 26 Oct 2010, 02:02 PM
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.
0
Veselin Vasilev
Telerik team
answered on 26 Oct 2010, 02:55 PM
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
0
Marc Roussel
Top achievements
Rank 2
answered on 26 Oct 2010, 03:02 PM
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
0
Accepted
Veselin Vasilev
Telerik team
answered on 26 Oct 2010, 03:15 PM
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
0
Marc Roussel
Top achievements
Rank 2
answered on 26 Oct 2010, 03:17 PM
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
0
Marc Roussel
Top achievements
Rank 2
answered on 26 Oct 2010, 03:22 PM
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);
    }
}
0
Marc Roussel
Top achievements
Rank 2
answered on 26 Oct 2010, 03:23 PM
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
0
Marc Roussel
Top achievements
Rank 2
answered on 26 Oct 2010, 03:48 PM
Ok forget it, I find it it's ((RadButton)sender).CommandParameter
0
Marc Roussel
Top achievements
Rank 2
answered on 26 Oct 2010, 03:52 PM
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 :
0
Marc Roussel
Top achievements
Rank 2
answered on 26 Oct 2010, 04:20 PM
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:
    }
}
Tags
GridView
Asked by
Marc Roussel
Top achievements
Rank 2
Answers by
Marc Roussel
Top achievements
Rank 2
Veselin Vasilev
Telerik team
Share this question
or