Completed
Last Updated: 26 Sep 2016 07:34 by ADMIN
ADMIN
Dess | Tech Support Engineer, Principal
Created on: 16 Aug 2016 07:17
Category: GridView
Type: Bug Report
0
FIX. RadGridView - NullReferenceException when rebinding the grid in the CellValueChanged event for a GridViewComboBoxColumn/GridViewMultiComboBoxColumn
To reproduce:

public Form1()
{
    InitializeComponent();

    GridViewComboBoxColumn comboCol = new GridViewComboBoxColumn();         
    comboCol.DataSource = InitComboActive();
    comboCol.ValueMember = "ActiveCode";
    comboCol.DisplayMember = "ActiveDsc";
    comboCol.FieldName = "ActiveCode";

    this.radGridView1.Columns.Add(comboCol);

    this.radGridView1.AutoGenerateColumns = false;
    BindRadGrid();
    this.radGridView1.CellValueChanged += radGridView1_CellValueChanged;
}

private void radGridView1_CellValueChanged(object sender, GridViewCellEventArgs e)
{
    BindRadGrid();
}

private void BindRadGrid()
{
    this.radGridView1.DataSource = null;
    this.radGridView1.DataSource = InitComboData();
}

private DataTable InitComboActive()
{
    DataTable dt = new DataTable("DtActive");
    dt.Columns.Add("ActiveCode");
    dt.Columns.Add("ActiveDsc");

    dt.Rows.Add("0", "InActive");
    dt.Rows.Add("1", "Active");

    return dt;
}

private DataTable InitComboData()
{
    DataTable dt = new DataTable("DtData");
    dt.Columns.Add("Host");
    dt.Columns.Add("ActiveCode");
    dt.Columns.Add("ActiveDsc");
    
    dt.Rows.Add("Host A", "0", "InActive");
    dt.Rows.Add("Host B", "1", "Active");

    return dt;
}

Workaround: use the RadGridView.CellEndEdit instead for rebinding. 
Workaround 2: use the CellValidated event:
private void radGridView1_CellValidated(object sender, CellValidatedEventArgs e)
{
    if (e.Row is GridViewDataRowInfo)
    {
        BindRadGrid();
    }
}

4 comments
ADMIN
Dess | Tech Support Engineer, Principal
Posted on: 23 Aug 2016 09:17
Hello Usadi, 

Thank you for writing back. 

If you rebind the grid in the CellValidated event while navigating from a data row to the new row, indeed an exception is thrown. It can be handled by a custom GridNewRowBehavior:
public RadForm1()
{
    InitializeComponent();
    
    BaseGridBehavior gridBehavior = radGridView1.GridBehavior as BaseGridBehavior;
    gridBehavior.UnregisterBehavior(typeof(GridViewNewRowInfo));
    gridBehavior.RegisterBehavior(typeof(GridViewNewRowInfo), new CustomGridNewRowBehavior());

    this.radGridView1.CellValidated += radGridView1_CellValidated;
}

public class CustomGridNewRowBehavior : GridNewRowBehavior
{
    protected override bool OnMouseDownLeft(System.Windows.Forms.MouseEventArgs e)
    { 
        GridNewRowElement row = GetRowAtPoint(e.Location) as GridNewRowElement;
        if (row == null)
        {
            return base.OnMouseDownLeft(e);
        }

        row.UpdateContentVisibility(true);
        if (row.RowInfo == null)
        {
            return true;
        }
       
        GridCellElement cell = this.GridViewElement.ElementTree.GetElementAtPoint(e.Location) as GridCellElement;
        this.Navigator.Select(row.RowInfo, cell.ColumnInfo);
        this.GridViewElement.BeginEdit();

        if (row.RowInfo != null && !row.RowInfo.IsCurrent)
        {
            row.UpdateContentVisibility(false);
            return this.GridViewElement.EndEdit();
        }

        return true;
    }
}

If you are experiencing any further difficulties, feel free reply in the support ticket that you have opened. Thank you.
Imported User
Posted on: 16 Aug 2016 15:28
Dear Dess, ur solution its well for handling message message box indicating that the columns doesn't belong to the table with add cellValidated,

but i test again with another condition, when editing data on the grid and then focus on add new row then move to next fieled, application become error NullReferenceException. how to hanndle it? this is intermittent, but the issue will be solve if i remove event binding on CellEndEdit, 

for your information this is my code inside ceelEventEdit

private void RadGridMsHost_CellEndEdit(object sender, GridViewCellEventArgs e)
        {
            if (e.Row is GridViewFilteringRowInfo)
            {
                BindRadGrid();
            }
            if (e.Row is GridViewDataRowInfo)
            {
                BindRadGrid;
            }
        }

in my snippet code  i was add binding o cell validated like this 
private void radGridView1_CellValidated(object sender, CellValidatedEventArgs e)
{
if (e.Row is GridViewDataRowInfo)
{
BindRadGrid();
}
if (e.Row is GridNewDataRowInfo)
{
BindRadGrid();
}
}

and still not running well.
ADMIN
Dess | Tech Support Engineer, Principal
Posted on: 16 Aug 2016 10:22
NOTE: If you enable filtering, activate the editor for cell, change the selection in the drop down editor and then click the filter cell, you will get a message box indicating that the columns doesn't belong to the table. That is why use the CellValidated event instead.
private void radGridView1_CellValidated(object sender, CellValidatedEventArgs e)
{
    if (e.Row is GridViewDataRowInfo)
    {
        BindRadGrid();
    }
}
Imported User
Posted on: 16 Aug 2016 09:30
i was used before on event CellEndEdit, but i found bugs too, base on my sample project below, please compile again and try to edit data on grid, change combox column, then foucs on grid "FILTER" column, will be trigger prompt of "Column 'ActiveCode' does not belong to table ", for this reason why i not prefer to update or rebind grid from CellEndEdit ", this message occurs only when editng data on grid and then try stright focus to Grid Filtering,

please tell me whats wrong?