RadGridView: Adding button in a column

2 Answers 877 Views
GridView
Zvi
Top achievements
Rank 1
Zvi asked on 28 Jan 2017, 04:07 AM

Hello,

The code in the attached image is used to display a table.

I want to add a new column in which each row has a button. 

Upon click, the handler will "know" the line and act accordingly.

Is it possible ?

Thank you,

Z.V

 

2 Answers, 1 is accepted

Sort by
0
al
Top achievements
Rank 1
answered on 29 Jan 2017, 08:57 AM

hi. you can define a button grid column as below:

public class MyButtonGridViewColumn : Telerik.Windows.Controls.GridViewColumn
    {
        public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
        {
            System.Windows.Controls.Button button = cell.Content as System.Windows.Controls.Button;
            if (button == null)
            {
                button = new System.Windows.Controls.Button();
                button.Content = "Calibrate";
                CorrelationCalibrationGrid grid=cell.ParentOfType<CorrelationCalibrationGrid>();
                button.Tag = "WHAT YOU NEED TO USE ON CLICK.IT CAN BE OBJECT";
                 
                button.Click += Button_Click;
 
            }
            return button;
        }
 
        //handle the calibrate button click event
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (sender is System.Windows.Controls.Button)
            {
                System.Windows.Controls.Button btn = sender as System.Windows.Controls.Button;
                if (btn.Tag != null && btn.Tag is NewClassForButtom )
                {
                    NewClassForButtom cls=btn.Tag as NewClassForButtom;
                    if (cls != null)
                    {
                        CorrelationCalibrationGrid grid = cls.grid;
                        CorrelationCalibrationGrid.CalibratableCorrelationClass correlationClass = cls.correlationClass;
                        correlationClass.calibrationWindowInputClass = CorrelationCalibrationGrid.CreateCorrelationCalibrationInputDataClass(correlationClass.calibrationWindowInputClass, grid.FluidForCorrelationCalibration);
                        (new CorrelationCalibrationWindow(correlationClass)).ShowDialog();
                    }
                }
            }
        }
    }
Zvi
Top achievements
Rank 1
commented on 31 Jan 2017, 09:04 PM

Hello,

Thank you very much !

How should I change my XAML ?

Best regards,

Z.V

al
Top achievements
Rank 1
commented on 31 Jan 2017, 09:16 PM

you can do just like adding common columns but inssteade of GridViewDataColumn use MyButtonGridViewColumn  like below:

<MyButtonGridViewColumn Name="buttonColumn1">
</MyButtonGridViewColumn >
0
Stefan Nenchev
Telerik team
answered on 01 Feb 2017, 10:59 AM
Hi,

You can also refer to the following article from our documentation page - Add a Button Column. It shows an additional approach of using the CellTemplate of the GridViewColumn to define the Button in XAML:

<telerik:GridViewColumn Header="Delete" >
       <telerik:GridViewColumn.CellTemplate>
           <DataTemplate>
               <telerik:RadButton Content="Delete"
                       Command="telerik:RadGridViewCommands.Delete"
                       CommandParameter="{Binding}" />
           </DataTemplate>
       </telerik:GridViewColumn.CellTemplate>
   </telerik:GridViewColumn>

Regards,
Stefan Nenchev
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
Anish
Top achievements
Rank 1
commented on 24 May 2018, 08:32 AM

How Do I get the selected row when this button is clicked?
Dilyan Traykov
Telerik team
commented on 28 May 2018, 10:53 AM

Hello Anish,

You can pass in the parent row by using a RelativeSource binding, like so:

     <telerik:GridViewColumn.CellTemplate>
         <DataTemplate>
             <telerik:RadButton Content="Delete"
Command="{Binding MyCommand, Source={StaticResource MyViewModel}}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=telerik:GridViewRow}}" />
         </DataTemplate>
     </telerik:GridViewColumn.CellTemplate>

You can then get ahold of both the actual row and the underlying item like this:

public ICommand MyCommand { get; set; }
 
public MyViewModel()
{
    this.MyCommand = new DelegateCommand(OnMyCommandExecuted);
}
 
private void OnMyCommandExecuted(object obj)
{
    var row = obj as GridViewRow;
    var item = row.DataContext;
}

Please let me know whether this is what you had in mind.

Regards,
Dilyan Traykov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Hüseyin
Top achievements
Rank 1
commented on 03 Jul 2018, 11:22 AM

Please hel me friends,

i want to when i click gridview cell, create new button to clicked inside cell.

i want to like picture.Please look my attach files(picture).

Martin Ivanov
Telerik team
commented on 04 Jul 2018, 07:20 AM

Hello Hüseyin,

I am not sure that I understand your requirement, but my guess here is that when you click a gridview cell you enter edit mode. If this is correct you can use the CellEditTemplate and define the button there. Note that the gridview will display the content from the CellTemplate when not in edit mode. And the content from the CellEditTemplate when in edit mode.

Regards,
Martin Ivanov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Hüseyin
Top achievements
Rank 1
commented on 05 Jul 2018, 09:45 AM

Thank Martin for your help.

I am a junior developer. I am use radgridviewTableData.ItemsSource = datatables . I want to When i clicked anywhere cell on radgridview, create e new button clicked cell.I want create button to the right of the cell. Please help because my boss is angry to me :)

Martin Ivanov
Telerik team
commented on 10 Jul 2018, 08:09 AM

Hello Hüseyin,

I attached a small example showing a possible approach for achieving your requirement. Can you please give it a try and let me know how it goes?

Regards,
Martin Ivanov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Hüseyin
Top achievements
Rank 1
commented on 10 Jul 2018, 04:33 PM

Thank you Martin.

You are superhero.I am happy. Very very thank you :) This program is successfull.

Martin Ivanov
Telerik team
commented on 11 Jul 2018, 08:55 AM

Hello Hüseyin,

I am glad to hear that the project was helpful.

Regards,
Martin Ivanov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Hüseyin
Top achievements
Rank 1
commented on 27 Jul 2018, 06:19 PM

Hi Martin,

i am working telerik and wpf. I use radgridview. I want :: When i press enter key on last cell, insert new row. But i dont know.

Please help me.

Martin Ivanov
Telerik team
commented on 30 Jul 2018, 01:53 PM

Hello Hüseyin,

Note that you can insert a new row into the gridview with the "Insert" key on the keyboard. If you want to insert a new row also on "Enter" key press, you can create a custom keyboard command provider. And then override its ProvideCommandsForKey() method. In the method you can add the BeginInsert command to the list with the default commands. Here is an example in code:
public class CustomKeyboardCommandProvider : DefaultKeyboardCommandProvider
{
    public CustomKeyboardCommandProvider(GridViewDataControl dataControl) : base(dataControl)
    {
    }
 
    public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
    {
        var commands = base.ProvideCommandsForKey(key);
        if (key == Key.Enter)
        {
            return new List<ICommand>() { RadGridViewCommands.BeginInsert };
        }
 
        return commands;
    }
}
I also prepared a small example showing this approach. I hope it helps.

Regards,
Martin Ivanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Hüseyin
Top achievements
Rank 1
commented on 31 Jul 2018, 09:49 AM

Thanks Martin,

This codes i very good. But i want:
1- When edit mode or not edit mode in a cell(anything cell), i press enter ---  Must be focus move next row(same column). 

2-) When i last row in a cell, i press enter  i press enter ---- Must be open new row :)

I want very help from you. So again thank you. 

Hüseyin
Top achievements
Rank 1
commented on 02 Aug 2018, 02:38 PM

public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
          {


              List<ICommand> commandsToExecute = base.ProvideCommandsForKey(key).ToList();

              if (key == Key.Enter || key == Key.Down)
              {

                  var lastRowIndex = radgridviewOrca.Items.Count - 1;
                  var isLastRow = radgridviewOrca.Items.IndexOf(radgridviewOrca.SelectedItem) == lastRowIndex;

                  if (isLastRow)
                  {
                      commandsToExecute.Clear();
                      if (radgridviewOrca.Items.IsEditingItem)
                      {

                          commandsToExecute.Add(RadGridViewCommands.CommitEdit);
                          commandsToExecute.Add(RadGridViewCommands.BeginInsert);
                          commandsToExecute.Add(RadGridViewCommands.BeginEdit);
                          radgridviewOrca.CurrentColumn = radgridviewOrca.Columns[0];
                      }
                      else
                      {
                          commandsToExecute.Add(RadGridViewCommands.BeginInsert);
                          radgridviewOrca.CurrentColumn = radgridviewOrca.Columns[0];
                      }

                      radgridviewOrca.CurrentItem = radgridviewOrca.SelectedItem;

                  }

                  radgridviewOrca.SelectedItem = radgridviewOrca.Items.CurrentItem;

              }
              return commandsToExecute;
          }

 

 

Hi Martin my code is here.

When i edit cell,i press enter, my gridview selecteditems is not on new row.İt is previous row.

My probleam is : https://www.telerik.com/forums/selecteditem-on-adding-new-row.

Please help me. 

 

 

Martin Ivanov
Telerik team
commented on 03 Aug 2018, 09:50 AM

Hi Hüseyin,

I will check your case and get back to you later today.

Regards,
Martin Ivanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Hüseyin
Top achievements
Rank 1
commented on 03 Aug 2018, 01:41 PM

Hi Martin

List<ICommand> commandsToExecute = base.ProvideCommandsForKey(key).ToList();
 
                if (key == Key.Enter || key == Key.Down)
                {
                    var lastRowIndex = radgridviewOrca.Items.Count - 1;
                    var isLastRow = radgridviewOrca.Items.IndexOf(radgridviewOrca.SelectedItem) == lastRowIndex;
 
                    if (isLastRow)
                    {
                        commandsToExecute.Clear();
                        if (radgridviewOrca.Items.IsEditingItem)
                        {
                            commandsToExecute.Add(RadGridViewCommands.CommitEdit);
                            commandsToExecute.Add(RadGridViewCommands.BeginInsert);
                            commandsToExecute.Add(RadGridViewCommands.MoveDown);
                            commandsToExecute.Add(RadGridViewCommands.BeginEdit);
                        }
                        else
                        {
                            commandsToExecute.Add(RadGridViewCommands.CommitEdit);
                            commandsToExecute.Add(RadGridViewCommands.BeginInsert);
                             
                             
                        }
                        radgridviewOrca.CurrentColumn = radgridviewOrca.Columns[0];
                        radgridviewOrca.CurrentItem = radgridviewOrca.SelectedItem;
                    }
                     
                    radgridviewOrca.CurrentItem = radgridviewOrca.SelectedItem;
                   // radgridviewOrca.SelectedItem = radgridviewOrca.Items.CurrentItem;
 
                }
                return commandsToExecute;

 

My Codes is here. I change IsSynchronizedWithCurrentItem="True". When i editing cell, i can new row. But my probleam is:

- When i am not editing cell(not edit mode), i press enter->> but can not new row :(

 

 

Stefan
Telerik team
commented on 08 Aug 2018, 11:14 AM

Hello Hüseyin,

I used the exact same implementation of the custom KeyboardCommandProvider in the sample application and on my end and the control inserts a new row irrelevant to editing  the item or not. When the user is not editing, the else statement with the BeginInsert command trigger will be processed so it is a bit strange that a new row is not inserted on your end. Can you please confirm that when the control is not in edit mode and the last item is selected, pressing the Enter key results in triggering the BeginInsert command?

Regards,
Stefan
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
gaurav
Top achievements
Rank 1
commented on 15 May 2019, 04:43 AM

Hello i am very new to telerik,

My question is how I can bind tiles to radtilelist from backend where my tile contains grid with buttons and content (Text).

 

 

I am attaching the screenshot as well.

 

Please let me know if there is any query.

Tags
GridView
Asked by
Zvi
Top achievements
Rank 1
Answers by
al
Top achievements
Rank 1
Stefan Nenchev
Telerik team
Share this question
or