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. Image for the cookie policy date

Null cell value after editing cell on gridgroupingcontrol bound to BindingList object

Using control from Essential Studio 14.4.0.15 with project references to control version 14.4451.0.15.

I've created a class with a couple of properties and bound a BindingList to a gridgroupingcontrol.

  internal class GenericConfiguration : INotifyPropertyChanged
  {
    //Implement INotifyPropertyChanged so that GridGroupingControl
    //in Configuration form can update the bound List<GenericConfiguration>.
    public event PropertyChangedEventHandler PropertyChanged;

    private dynamic _settingValue;

    public string SettingName { get; set; }
    public dynamic SettingValue
    {
      get { return _settingValue; }
      set 
      {
        bool bVal;
        if (bool.TryParse(value, out bVal)) {
          if (_settingValue != bVal) {
            _settingValue = bVal;
            RaisePropertyChanged();
          }
        }
        else if (_settingValue != value) {
          _settingValue = value;
          RaisePropertyChanged();
        }
      }
    }

    public GenericConfiguration() {}

    public GenericConfiguration(string Name, dynamic Value)
    {
      this.SettingName = Name;
      _settingValue = Value;
    }

    void RaisePropertyChanged([CallerMemberName] string PropName = "")
    {
      PropertyChangedEventHandler handlerPC = PropertyChanged;
      if (handlerPC != null) {
        handlerPC(this, new PropertyChangedEventArgs(PropName));
      }
    }
  }

I run my project and pull up the Configuration screen.  When I try to edit a SettingValue cell, the value I had just typed is replaced by a null value after clicking another cell.  I've tried looking for a solution here and elsewhere without success.  I've even looked at a demo using this control from one of your knowledgebase articles.  I've tried running tests with and without QueryCellStyleInfo event handler.  Funny thing is that with the QueryCellStyleInfo event handler, the gridgroupingcontrol does pass the changed value on a cell with a dropdown but on a cell without a dropdown it passes 'null' (see pics at the bottom).

Below is the code for my Configuration form:

  public partial class Configuration : Form
  {

    BindingList<GenericConfiguration> _lstSettings = new BindingList<GenericConfiguration>();
    StringCollection _lstBools = new StringCollection { "true", "false" };


    public Configuration()
    {
      InitializeComponent();
    }

    private void Configuration_Load(object sender, EventArgs e)
    {
      _lstSettings = Global.AppConfiguration.GetConfigDisplaySettingsListTest();
      ggcSettings.DataSource = _lstSettings;
      ggcSettings.ShowColumnHeaders = true;
      ggcSettings.BackColor = Color.SandyBrown;
      ggcSettings.TableOptions.ListBoxSelectionMode = SelectionMode.One;
      
    }


    private void ggcSettings_QueryCellStyleInfo(object Sender, GridTableCellStyleInfoEventArgs e)
    {
      Debug.Print("r: " + e.Style.TableCellIdentity.RowIndex + " c: " + e.Style.TableCellIdentity.ColIndex + "  val: " + e.Style.CellValue);
      //string d = "row: {0}  col: {1} - {2}";
      //Debug.Print(string.Format(d, e.TableCellIdentity.RowIndex, e.TableCellIdentity.ColIndex, e.Style.CellValue.ToString() + ""));

      if (e.TableCellIdentity.RowIndex > 1)
      {
        if (e.TableCellIdentity.ColIndex == 1)
        {
          e.Style.Enabled = false;
        }
        if (e.TableCellIdentity.ColIndex == 2)
        {
          if (e.Style.CellValue != null)
            if ((e.Style.CellValue.ToString() != ""))
            {
              if (_lstSettings[e.TableCellIdentity.RowIndex - 2].SettingValue.GetType() == typeof(Boolean))
              {
                e.Style.CellType = "ComboBox";
                e.Style.ChoiceList = _lstBools;
                e.Style.DropDownStyle = Syncfusion.Windows.Forms.Grid.GridDropDownStyle.AutoComplete;
                e.Style.AutoCompleteInEditMode = Syncfusion.Windows.Forms.Grid.GridComboSelectionOptions.AutoComplete;
              }
              else
              {
                e.Style.CellType = "TextBox";
                e.Style.ReadOnly = false;

              }

            }
        }

      }
    }

  }

Below are my testing pics when NOT using QueryCellStyleInfo handler:

 
 

Below are testing pics when using QueryCellStyleInfo event handler:
 
 


Your help is much appreciated.


3 Replies

MG Mohanraj Gunasekaran Syncfusion Team July 31, 2017 10:33 AM UTC

Hi Charles,  
 
Thanks for using Syncfusion product.  
 
By default, GridGroupingControl does not have the support for dynamic type of field in datasource collection. In order to overcome this scenario, you can use the SaveCellFormattedText event. Please refer to the below code example and sample,  
  
Code example  
this.gridGroupingControl1.SaveCellFormattedText += GridGroupingControl1_SaveCellFormattedText;  
  
private void GridGroupingControl1_SaveCellFormattedText(object sender,GridCellTextEventArgs e)  
{  
    bool value;  
    if (bool.TryParse(e.Text, out value))  
        e.Style.CellValueType = typeof(bool);  
    else  
        e.Style.CellValueType = typeof(string);  
}  
  
Sample link: GridGroupingControl  
 
Regards,  
Mohanraj G  
 



CC Charles Chacon July 31, 2017 07:25 PM UTC

Mohanraj,

Outstanding!  So, basically the control needs to have the types defined in e.Style.CellValueType.

What I did to simplify my code was to assign the types in QueryCellStyleInfo event and now I don't need to Parse for a boolean when setting the GenericConfiguration.SettingValue property:

    private void ggcSettings_QueryCellStyleInfo(object Sender, GridTableCellStyleInfoEventArgs e)

    {

      Debug.Print("r: " + e.Style.TableCellIdentity.RowIndex + " c: " + e.Style.TableCellIdentity.ColIndex + "  val: " + e.Style.CellValue);

      //string d = "row: {0}  col: {1} - {2}";

      //Debug.Print(string.Format(d, e.TableCellIdentity.RowIndex, e.TableCellIdentity.ColIndex, e.Style.CellValue.ToString() + ""));


      if (e.TableCellIdentity.RowIndex > 1)

      {

        if (e.TableCellIdentity.ColIndex == 1)

        {

          e.Style.Enabled = false;

        }

        if (e.TableCellIdentity.ColIndex == 2)

        {

          if (e.Style.CellValue.ToString() != "")

          {

            if (_lstSettings[e.TableCellIdentity.RowIndex - 2].SettingValue.GetType() == typeof(Boolean))

            {

              e.Style.CellType = "ComboBox";

              e.Style.CellValueType = typeof(Boolean);

              e.Style.ChoiceList = _lstBools;

              e.Style.DropDownStyle = Syncfusion.Windows.Forms.Grid.GridDropDownStyle.AutoComplete;

              e.Style.AutoCompleteInEditMode = Syncfusion.Windows.Forms.Grid.GridComboSelectionOptions.AutoComplete;

            }

            else

            {

              e.Style.CellValueType = _lstSettings[e.TableCellIdentity.RowIndex - 2].SettingValue.GetType();

            }


          }

          else

          {

            e.Style.CellValueType = _lstSettings[e.TableCellIdentity.RowIndex - 2].SettingValue.GetType();

          }

        }


      }

    }


  internal class GenericConfiguration : INotifyPropertyChanged

  {

  ...
    public dynamic SettingValue
    {
      get { return _settingValue; }
      set 
      {
        if (_settingValue != value)
        {
          _settingValue = value;
          RaisePropertyChanged();
        }
      }
    }
   ...
   }

Thanks very much for the help!




MG Mohanraj Gunasekaran Syncfusion Team August 1, 2017 07:00 AM UTC

Hi Charles, 

Thanks for your update. 

Yes, you can also set the type for fields using QueryCellStyleInfo event.  

Anyway, we are glad to know that your reported problem has resolved. 

Please let us know, if you have any concerns. 

Regards, 
Mohanraj G 


Loader.
Live Chat Icon For mobile
Up arrow icon