Skipping ReadOnly Columns When Editing C1FlexGrid

In C1FlexGrid for Silverlight, you can easily edit data in cells and commit the changes by pressing enter or Tab key to move to the next cell. However, there are scenarios when you don't want your users to be able to edit some specific columns (for eg. Id). When you edit a cell and press enter to commit the changes, the focus moves to the next cell irrespective of the fact that the next cell is read only or not. Wouldn't it be nice if you could just keep on editing data, without having to bother about ReadOnly cells and move to the next Editable cell? It is a simple implementation which I've explained here.

Binding C1FlexGrid and Specifying ReadOnly Columns

I'm sure you're familiar with binding C1FlexGrid to data. Here's a quick sample for those who are not. Create a class Product which implements the INotifyPropertyChanged interface and create an IEnumerable List of this class. Then set this list as the ItemsSource of C1FlexGrid.


public class GridData  
{  
        public static IEnumerable<Product> GetProducts()  
        {  
            List<Product> result = new List<Product>();  

            Product obj;  
            for (int i = 0; i < 20; i++)  
            {  
                obj = new Product()  
                {  
                    ID = i,  
                    Name = "Product " + i,  
                    StandardCost = i * 100,  
                    Available = 1 % 2 == 0 ? true : false,  
                    ProductNumber = "Prod_" + i  
                };  

                result.Add(obj);  
            }  
            return result;  
        }  
    }  

c1FlexGrid1.ItemsSource = GridData.GetProducts();  

You could define the column as ReadOnly in code behind if columns are AutoGenerated or in xaml itself.


c1FlexGrid1.Columns[2].IsReadOnly = true;  
c1FlexGrid1.Columns[3].IsReadOnly = true;  

Skipping ReadOnly Cells

To skip ReadOnly cells and move the focus to the next editable column, we need to handle the KeyUp event of C1FlexGrid. In this event, we check if the column in focus is ReadOnly and if it is move the focus to the next Editable column. Also, we need to check if the next column is the last column and it is ReadOnly, then move focus to the next row.


private void c1FlexGrid1_KeyUp(object sender, KeyEventArgs e)  
        {  
            if (e.Key == Key.Enter || e.Key == Key.Tab)  
            {  
                int lastColumn = c1FlexGrid1.Selection.Column;  
                int colCount = c1FlexGrid1.Columns.Count;  

                if (lastColumn == colCount - 1)  
                {  
                    if (c1FlexGrid1.Columns[lastColumn].IsReadOnly)  
                    {  
                        c1FlexGrid1.StartEditing(true, c1FlexGrid1.Selection.Row + 1, 1);  
                    }  
                    else  
                    {  
                        c1FlexGrid1.StartEditing(true, c1FlexGrid1.Selection.Row, c1FlexGrid1.Selection.Column);  
                    }  
                }  
                else  
                {  
                    for (int i = c1FlexGrid1.Selection.Column; i < colCount; i++)  
                    {  
                        if (c1FlexGrid1.Columns[i].IsReadOnly == false)  
                        {  
                            c1FlexGrid1.StartEditing(true, c1FlexGrid1.Selection.Row, i);  
                            break;  
                        }  
                    }  
                }  
            }  
        }  

Conclusion

This article explains how you can skip the ReadOnly cells while editing. This can be achieved in C1FlexGrid for Winforms as well which is explained here. Please download the attached sample for complete implementation. Download Sample

GrapeCity

GrapeCity Developer Tools
comments powered by Disqus