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
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();
}
}
}
}
}
Hello,
Thank you very much !
How should I change my XAML ?
Best regards,
Z.V
you can do just like adding common columns but inssteade of GridViewDataColumn use MyButtonGridViewColumn like below:
<
MyButtonGridViewColumn
Name
=
"buttonColumn1"
>
</
MyButtonGridViewColumn
>
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
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
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).
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
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 :)
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
Thank you Martin.
You are superhero.I am happy. Very very thank you :) This program is successfull.
I am glad to hear that the project was helpful.
Regards,
Martin Ivanov
Progress Telerik
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.
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;
}
}
Regards,
Martin Ivanov
Progress Telerik
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.
{
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.
I will check your case and get back to you later today.
Regards,
Martin Ivanov
Progress Telerik
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 :(
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
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.