Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
45
How to customize the cell editing behavior?
posted

Hi everyone,

I'm currently working with UltraGrid in a Windows Forms application and I'm looking to customize the cell editing behavior. Specifically, I want to: 

1. Enable editing for specific columns only.
2. Use different editors (e.g., dropdowns, date pickers) for different columns.
3. Validate the input data before committing the changes.

Could anyone provide a step-by-step guide or examples on how to achieve this? Any tips on best practices for handling cell editing in UltraGrid would also be greatly appreciated! drift boss

Thanks in advance for your help!

  • 1700
    Offline posted

    Hello Rashford,

    To allow editing only in specific columns, you can use the CellActivation property for each column. Set this property to Activation.AllowEdit for columns you want to be editable and Activation.NoEdit for those you want to keep read-only. Below you can find a code snippet demonstrating this:

     

    // Access the UltraGridColumn
    UltraGridColumn column = ultraGrid.DisplayLayout.Bands[0].Columns["YourColumnName"];
    
    // Allow editing for this column
    column.CellActivation = Activation.AllowEdit;
    
    // Disable editing for another column
    ultraGrid.DisplayLayout.Bands[0].Columns["AnotherColumnName"].CellActivation = Activation.NoEdit;
    

    You can assign different editors (like dropdowns, date pickers) to specific columns using EditorComponent or by setting the column's Style property.

     

    For a dropdown (using ValueList): You can use a ValueList for dropdown values in a column:

     

    // Create a ValueList
    ValueList valueList = new ValueList();
    valueList.ValueListItems.Add("Option1", "DisplayText1");
    valueList.ValueListItems.Add("Option2", "DisplayText2");
    
    // Assign the ValueList to the column
    ultraGrid.DisplayLayout.Bands[0].Columns["YourDropdownColumn"].ValueList = valueList;
    

    For a date picker: You can set the Style of the column to Date:

     

    ultraGrid.DisplayLayout.Bands[0].Columns["DateColumn"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Date;
    

    For numeric input:

     

    ultraGrid.DisplayLayout.Bands[0].Columns["NumericColumn"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Integer;
    

    Regarding validating the data of a cell, you can handle the BeforeCellUpdate or BeforeRowUpdate events to validate the data before committing it, below I am sharing a code snippet that demonstrates it:

     

    private void ultraGrid_BeforeCellUpdate(object sender, BeforeCellUpdateEventArgs e)
    {
        // Get the column being edited
        UltraGridColumn column = e.Cell.Column;
        
        // Validate input for a specific column
        if (column.Key == "YourColumnName")
        {
            if (!IsValidInput(e.NewValue))
            {
                // Cancel the update if validation fails
                e.Cancel = true;
                MessageBox.Show("Invalid input!");
            }
        }
    }
    
    // Sample validation method
    private bool IsValidInput(object value)
    {
        // Add your validation logic here
        return value != null && value.ToString().Length > 0;
    }
    

    Please let me know if you need any further assistance.

    Regards,
    Ivan Kitanov