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

How to add button column to telerik radgridview for WPF programatically

11 Answers 829 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Manish Gada
Top achievements
Rank 1
Manish Gada asked on 01 Jun 2010, 12:30 PM

Hi,

i am using the namespace - Telerik.Windows.Controls(2009.3.1208.35)

How to add button column to telerik radgridview for WPF programatically
and how to fire the event.

Thank you,
Shashi

11 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 01 Jun 2010, 02:26 PM
Hi Manish Gada,

Please take a look at this forum thread.
 

All the best,
Maya
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
Shashiram
Top achievements
Rank 1
answered on 02 Jun 2010, 07:07 AM
Hi,

i am completely new to WPF, so please provide me the code.

Thank you,
shashi
0
Veselin Vasilev
Telerik team
answered on 02 Jun 2010, 08:07 AM
Hi Shashiram,

I already replied to your support ticket:

Here is a sample project. It shows how to inherit GridViewColumn and override the CreateCellElement method to return the object you want in code behind.

Hope it helps.


Sincerely yours,
Veskoni
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
Shashiram
Top achievements
Rank 1
answered on 02 Jun 2010, 09:28 AM
Hi,
thank you for the reply.

we are using telerik controls version - 2009.3.1208.35

but your sample contains - version 2010.1.601.35

here the problem is i am not finding the class "RadGridViewCommands".

please provide me the code for the i am using

Thank you,
Shashi


0
Veselin Vasilev
Telerik team
answered on 02 Jun 2010, 01:37 PM
Hello Shashiram,

This project should work with the older assemblies as well.

Best wishes,
Veskoni
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
Shashiram
Top achievements
Rank 1
answered on 04 Jun 2010, 06:44 AM
Hi Veskoni,

we have updated our dlls and now i am able to add the button to grid.
pls see the following code i used.

public class MyDeleteButtonColumn : Telerik.Windows.Controls.GridViewColumn
    {
        public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
        {
            RadButton btn = new RadButton()
            {
                Content = "Delete",
                Command = RadGridViewCommands.Delete,
                CommandParameter = dataItem

            };
            return btn;
        }
    }


private void AddButtonColumnToGrid(RadGridView rdgv)
        {
            MyDeleteButtonColumn col = new MyDeleteButtonColumn();
            col.Header = "Action";
            rdgv.Columns.Add(col);
        }

Here i want to handle the button click event in the code to do some processing and then call the DB proc.

please provide me the code to handle the delete the button click event in the code.

thanks in advance.


Thank you,
Shashi


0
Veselin Vasilev
Telerik team
answered on 04 Jun 2010, 07:40 AM
Hi Shashiram,

Please check the latest project I sent you. It shows how to subscribe to the Click event of the button and execute some code in the event handler.

Hope it helps.

Kind regards,
Veskoni
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
Shashiram
Top achievements
Rank 1
answered on 04 Jun 2010, 08:07 AM
Hi,

i can see the following code in mainwindow.xaml.cs
pls tell the code location/location file for button event


namespace ButtonColumn
{
 /// <summary>
 /// Interaction logic for MainWindow.xaml
 /// </summary>
 public partial class MainWindow : Window
 {
  public MainWindow()
  {
   ICommand deleteCommand = RadGridViewCommands.Delete;

   InitializeComponent();   
   gridView.ItemsSource = (from i in Enumerable.Range(0, 10)
         select new Item { ID = i,
               Text = "item " + i.ToString(),
               Date = DateTime.Now.AddDays(i),
               IsActive = i % 3 == 0 ? true : false
         }).ToList();

   MyDeleteButtonColumn col = new MyDeleteButtonColumn();
   col.Header = "created at runtime" ;
   gridView.Columns.Add(col);
  }
 }

 public class Item
 {
  public int ID { get; set; }
  public string Text { get; set; }
  public DateTime Date { get; set; }
  public bool IsActive { get; set; }
 }

 public class MyDeleteButtonColumn : Telerik.Windows.Controls.GridViewColumn
 {
  public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
  {
   RadButton btn = new RadButton() { Content = "delete",
            Command = RadGridViewCommands.Delete,
            CommandParameter = dataItem };
   return btn;
  }
 }
}


Thank you,
Shashi

0
Veselin Vasilev
Telerik team
answered on 04 Jun 2010, 08:14 AM
Hi Shashiram,

I attached a second project in my reply from 02-June. You can download it from here as well.

The code looks like this:

public class MyDeleteButtonColumn : Telerik.Windows.Controls.GridViewColumn
{
    public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
    {
        RadButton btn = new RadButton() { Content = "delete" };
        btn.Click += new RoutedEventHandler(btn_Click);
        return btn;
    }
  
    void btn_Click(object sender, RoutedEventArgs e)
    {
        Button btn = sender as Button;
        RadGridView grid = btn.ParentOfType<RadGridView>();
        Item itemToDelete = btn.DataContext as Item;
  
        if (grid != null || itemToDelete != null)
        {
            grid.Items.Remove(itemToDelete);
        }
    }
}


Kind regards,
Veskoni
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
Shashiram
Top achievements
Rank 1
answered on 04 Jun 2010, 08:30 AM
Hi Veskoni ,

i am adding button using the following code. Is it possible to handel the event as in the following code
if yes please provide me the changes i need to do in the MyDeleteButtonColumn class and in the following method.
AddButtonColumnToGrid() is in the other class.

private void AddButtonColumnToGrid(RadGridView rdgv)
        {
            MyDeleteButtonColumn col = new MyDeleteButtonColumn();
            col.Header = "Action";
            col.UniqueName = "Action";
            col.MouseLeftButtonDown += new MouseButtonEventHandler(col_MouseLeftButtonDown);
            rdgv.Columns.Add(col);
        }

Thank you,
Shsahi
0
hart
Top achievements
Rank 1
Veteran
answered on 02 Aug 2013, 06:39 PM
Do this instead, 
There are two snippets here - the MyButtonColumn class with a Delagate, and then how you substantiate it and trap on the event (which is in your code section of your XAML file)
First, the MyButtonColumn class
public class MyButtonColumn : GridViewColumn
{
 
 public event JustifySelectedDelegate OnJustifySelectedHandler;
 
 protected virtual void OnJustifySelectedEvent()
 {
  if (OnJustifySelectedHandler != null)
   OnJustifySelectedHandler(this);
 }
 
 public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
 {
  RadButton button = cell.Content as RadButton;
  if (button == null)
  {
   button = new RadButton();
   button.Content = "Justify";
   button.Click += new RoutedEventHandler(button_Click);
  }
 
  button.CommandParameter = dataItem;
 
  return button;
 }
 
 void button_Click(object sender, RoutedEventArgs e)
 {
  OnJustifySelectedEvent();
 }
 
}

Now, how you consume it in your main code..... I am using a dataAdapter to fill my DataGrid
private void grdMachineParms_DataLoaded(object sender, EventArgs e)
{
 if (grdMachineParms.Columns.Last<Telerik.Windows.Controls.GridViewColumn>() is MyButtonColumn)
  return;
 MyButtonColumn aCol = new MyButtonColumn();
 aCol.OnJustifySelectedHandler += new JustifySelectedDelegate(aCol_OnJustifySelectedHandler);
 this.grdMachineParms.Columns.Add(aCol);
}
 
void aCol_OnJustifySelectedHandler(object sender)
{
 throw new NotImplementedException();
}
Tags
GridView
Asked by
Manish Gada
Top achievements
Rank 1
Answers by
Maya
Telerik team
Shashiram
Top achievements
Rank 1
Veselin Vasilev
Telerik team
hart
Top achievements
Rank 1
Veteran
Share this question
or