We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Strange bug with Grid and template columbs

Thread ID:

Created:

Updated:

Platform:

Replies:

144228 Apr 25,2019 03:53 PM UTC May 9,2019 09:59 AM UTC WPF 10
loading
Tags: SfDataGrid
Tom
Asked On April 25, 2019 03:53 PM UTC

I found a really weird bug with the grid,
I am creating a couple of columbs dynamically to add to an already defined grid.

var newCol = new Syncfusion.UI.Xaml.Grid.GridTemplateColumn();
newCol.HeaderText = "header"
newCol.HeaderTemplate = (DataTemplate)Grid1.FindResource("HeaderTemplate");
newCol.CellTemplate = (DataTemplate)Grid1.FindResource("CellTemplate");

the templates are defined in xmal like this

 <DataTemplate x:Key="HeaderTemplate">
                <StackPanel Orientation="Horizontal">
                    <Image Source="/Resources/_icon.png" />
                    <TextBlock Text="{Binding}"  />
                </StackPanel>               
            </DataTemplate>
            <DataTemplate x:Key="CellTemplate">
                <Grid VerticalAlignment="Center" HorizontalAlignment="Center">
                    <CheckBox   
                          />
                </Grid>                
            </DataTemplate>

Now lets say i have 30 rows in my grid, when the program opens, only 10 might be visible. if i click the checkbox on say rows 1,2,3 .. if i then scroll down rows 11,12,13 are also checked, as well as 21,22,23

I don't know how to describe it better than that; i can privately send a screen recorder video to demonstrate if it helps to fix it?

Thanks, Tom

Dinesh Babu Yadav [Syncfusion]
Replied On April 26, 2019 12:21 PM UTC

Hi Tom, 
 
We have analyzed the reported issue with template column based on the provided code snippet. But we are unable to reproduce the reported issue with checkbox check/uncheck.  
 
Please find the tested sample from the below link 
 
 
If you still are facing the same issue, please revert us with the modified sample in order to reproduce the reported issue from our end. It will helps us to investigate further and provide appropriate solution at earlier. 
 
Regards, 
Dinesh Babu Yadav. 
 


Tom
Replied On April 26, 2019 06:51 PM UTC

thanks,

so in your sample,

replace        

<CheckBox IsChecked="{Binding IsClosed}"/>

with 

 <CheckBox />

ie. just take out the binding .. and the error appears for me

(i dont want it bound because i want to handle the check function for another purpose).

Thanks.


Dinesh Babu Yadav [Syncfusion]
Replied On April 29, 2019 08:26 AM UTC

Hi Tom, 
 
We are able to reproduce the reported issue when checkbox is not bounded with any property. As per our control architecture, while scrolling, the row elements will be reused. In your case, since the CheckBox is not bounded with any property, it is updated with incorrect value. To resolve this issue, you must bind checkbox with the underlying property.  
 
Also please let us know, for what purpose you did not want to handle check function. It will helps us to investigate further and provide appropriate solution at earlier. 
 
Regards, 
Dinesh Babu Yadav. 
 


Tom
Replied On May 1, 2019 12:38 PM UTC

Thank you,
"To resolve this issue, you must bind checkbox with the underlying property.  " - that is not resolving the issue tho, as I have no underlying property to bind to. I can understand why this 'architecture' might make sense for defined columns like checkbox that will throw an exception if not bound, but the whole point of a template column is that the user can do their own thing, and to reuse those controls across rows is a dangerous bug. 
My purpose for handling the check function is I want to set/unset a property on another object with complex logic which is not possible to access via binding - I have found a work around to this for now using a Binding Convertor but it is convoluted and messy, and would be much easier if the TemplateColumn could behave as it should!


Dinesh Babu Yadav [Syncfusion]
Replied On May 2, 2019 09:51 AM UTC

Hi Tom, 
 
As mentioned earlier, as per our control architecture, we have reused the row control based on the underlying row data. And we have been reusing the row elements in order to achieve better performance, in which we have created row elements only for the rows that are in view and for the rest of the rows, we have reused the row element and replaced content with the new row data when they come in view. This is the performance strategy we are using for our SfDataGrid control. And if you are not binding anything, the cell values supposed to be repeated while scrolling as in your case. As you mentioned in your update, you can also bind converter instead of binding underlying property to resolve the reported issue. 
 
And we regret to inform you that, you cannot resolve the reported issue without binding to any property/converter to the CheckBox with our control architecture.  
 
Regards, 
Dinesh Babu Yadav. 
 


Tom
Replied On May 3, 2019 08:59 AM UTC

Ok .. well it should be made very clear in the documentation that it is doing this. Also preferably rename it from TemplateColumn to something different, as it does not function as a TemplateColumn with this behaviour. Better would be an option to turn off the reusing of row elements for cases where performance benefits are not needed.

Shobika Palani [Syncfusion]
Replied On May 6, 2019 02:03 PM UTC

Hi Tom, 

We regret for the inconvenience caused. 

We are currently validating the alternate way to achieve your requirement. We will update you with further details on 7th May 2019. 

We will appreciate your patience until then. 

Regards, 
Shobika. 


Shobika Palani [Syncfusion]
Replied On May 7, 2019 01:49 PM UTC

Hi Tom, 
 
On further analyzing, we have found that your can achieve your requirement using UnBoundColumns. Further, GridTemplateColumn is used for displaying the data based on the template used. Whereas, in your case, you do not want to bind the column with the underlying property  which can be accomplished by UnBound columns ,which are not bound with the data object from the underlying data source.  
 
Please refer to the below code snippet 
XAML: 
<syncfusion:GridUnBoundColumn MappingName="Closed" SetCellBoundValue="True"> 
     <syncfusion:GridUnBoundColumn.CellTemplate> 
          <DataTemplate> 
              <CheckBox HorizontalAlignment="Center" IsChecked="{Binding Value}"   
                        syncfusion:FocusManagerHelper.FocusedElement="True" 
                        VerticalAlignment="Center" Checked="CheckBox_Checked"/> 
          </DataTemplate> 
     </syncfusion:GridUnBoundColumn.CellTemplate> 
 </syncfusion:GridUnBoundColumn> 
 
C#: 
public partial class MainWindow : Window 
    { 
        Dictionary<object, bool?> checkBoxValues = new Dictionary<object, bool?>(); 
 
        public MainWindow() 
        { 
            InitializeComponent(); 
            this.dataGrid.QueryUnboundColumnValue += DataGrid_QueryUnboundColumnValue; 
        } 
 
        private void DataGrid_QueryUnboundColumnValue(object sender, GridUnboundColumnEventsArgs e) 
        { 
            if (e.UnBoundAction == UnBoundActions.QueryData) 
            { 
                if (checkBoxValues.ContainsKey(e.Record)) 
                    e.Value = checkBoxValues[e.Record]; 
            } 
        } 
 
        private void CheckBox_Checked(object sender, RoutedEventArgs e) 
        { 
            var checkBox = sender as CheckBox; 
            if (checkBox != null && this.dataGrid.CurrentItem != null) 
            { 
                if (!checkBoxValues.ContainsKey(this.dataGrid.CurrentItem)) 
                    checkBoxValues.Add(this.dataGrid.CurrentItem, checkBox.IsChecked); 
                checkBoxValues[this.dataGrid.CurrentItem] = checkBox.IsChecked; 
                this.dataGrid.UpdateUnboundColumn(this.dataGrid.CurrentItem, "Closed"); 
            } 
        } 
    } 

Please find sample for the same from the below link 
Sample Link: 

Please refer to the below UG link to know more details on GridTemplateColumn and GridUnboundColumn 
UG Link: 


Please let us know, if you need any further assistance on this. 

Regards, 
Shobika. 


Tom
Replied On May 8, 2019 05:09 PM UTC

Thank-you that's brilliant.

For anyone else with same need, the sample works for me, but in my application this.dataGrid.CurrentItem was always null so I had to change it to (( Syncfusion.UI.Xaml.Grid.Cells.DataContextHelper) checkBox.DataContext).Record
Also I had to handle the unbounded function too, for when user unchecks it - with same function as checked.

Dinesh Babu Yadav [Syncfusion]
Replied On May 9, 2019 09:59 AM UTC

Hi Tom, 
 
Thanks for the update. Please let us know if you require further assistance. 
 
Regards, 
Dinesh Babu Yadav 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon