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

Ria Services autogenerated checkbox save on click

3 Answers 44 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 28 Apr 2011, 02:25 PM
Hi,

I am using the radgridview to display a collection of ria services entities that are autogenerated.  Included in the entity is a boolean that is rendered via an autogenerated checkbox.  Currently if I check or uncheck the checkbox, I have to move focus away from the checkbox in order for the new value to be updated on the entity.  I would like for the value to be updated on the entity immediately upon checking or unchecking the checkbox (so that I can immediately perform other actions that affect other entities in the collection).  How can I accomplish this?

Thanks

3 Answers, 1 is accepted

Sort by
0
Ivan Ivanov
Telerik team
answered on 02 May 2011, 02:18 PM
Hi Chris,

I have prepared an example project for you case. Please, refer to it and inform us if this approach meets your requirements.

All the best,
Ivan Ivanov
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
Chris
Top achievements
Rank 1
answered on 02 May 2011, 04:25 PM

Hi Ivan,

Thanks for the sample.  Your sample works well, but it depends on defining a CellEditTemplate.  Because I am using autogeneration, I do not have CellEditTemplates defined.  I was hoping that I could get to the cell instanciated checkbox and hook the Click event, but I have not been able to in a couple scenarios:

1. I tried to hook the RowLoaded event on the grid and execute the following code:

if (cell.Content is CheckBox)
{
 (cell.Content as CheckBox).Click += new RoutedEventHandler(DataEntityGridView_Click);
}

In this case the click event does not fire when being clicked.

2. I tried to hook the BeginningEdit and CellEditEnding events to hook and unhook the Click event:

private void DataGrid_BeginningEdit(object sender, GridViewBeginningEditRoutedEventArgs e)
{
 if (e.Cell.Content is CheckBox)
 {
  (e.Cell.Content as CheckBox).Click += DataEntityGridView_Click;
 }
}

private void DataGrid_CellEditEnded(object sender, GridViewCellEditEndedEventArgs e)
{
 if (e.Cell.Content is CheckBox)
 {
  (e.Cell.Content as CheckBox).Click -= DataEntityGridView_Click;
 }
}

The click event did not fire in this case either.

Any other ideas?

Thanks,
Chris

0
Chris
Top achievements
Rank 1
answered on 02 May 2011, 07:14 PM
I came up with a solution that seems to work:

1. I created a new control (CommitOnClickCheckBox) that inherits from CheckBox and implements the Click event in the way your sample suggested:

private void CheckBox_Click(object sender, RoutedEventArgs e)
{
 CheckBox checkbox = sender as CheckBox;
 if (checkbox.ParentOfType<GridViewCell>().IsInEditMode)
 {
  checkbox.ParentOfType<GridViewCell>().CommitEdit();
 }
}

2. In the datagrid AutoGeneratingColumn event I detect that the column needs to implement the "commit on click" behavior and set the CellEditTemplate to a dynamically created data template that defines the CommitOnClickCheckBox with the binding to the passed field name:

e.Column.CellEditTemplate = CreateCommitOnClickCheckboxDataTemplate();

private DataTemplate CreateCommitOnClickCheckboxDataTemplate(string fieldName)
{
 string xaml = @"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007"""
  + @" xmlns:mynamespace=""http://mycompany/mynamespace"">"
  + @"<mynamespace:CommitOnClickCheckBox IsChecked=""{Binding " + fieldName + @", Mode=TwoWay}"" /></DataTemplate>";
 return (DataTemplate) XamlReader.Load(xaml) as DataTemplate;
}

 

This implementation solves my problem.  I am sharing in case it is useful to anyone else.

-Chris
Tags
GridView
Asked by
Chris
Top achievements
Rank 1
Answers by
Ivan Ivanov
Telerik team
Chris
Top achievements
Rank 1
Share this question
or