This is a migrated thread and some comments may be shown as answers.

RadGrid Re-Bind Exception

3 Answers 201 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Guhan Swaminathan
Top achievements
Rank 1
Guhan Swaminathan asked on 16 Jun 2010, 08:12 AM
Hi,

When i try to re-bind the datasource in RadGridView  Selection Changed event  throws  the below exception :  System.NullReferenceException: Object reference not set to an instance of an object. Pls any solution for this.


SampleCode :

using

 

System;

 

using

 

System.Collections.Generic;

 

using

 

System.ComponentModel;

 

using

 

System.Data;

 

using

 

System.Drawing;

 

using

 

System.Text;

 

using

 

System.Windows.Forms;

 

using

 

Telerik.WinControls;

 

using

 

Telerik.WinControls.UI;

 

namespace

 

RadGrid

 

{

 

public partial class RadForm1 : Telerik.WinControls.UI.RadForm

 

{

 

DataTable dtUserDetails = new DataTable();

 

 

public RadForm1()

 

{

InitializeComponent();

radGridView1.GridBehavior =

new GridBehavior();

 

}

 

private void RadForm1_Load(object sender, EventArgs e)

 

{

 

dtUserDetails.Columns.Add(

"ID");

 

dtUserDetails.Columns.Add(

"Name");

 

 

DataRow newRow;

 

 

for (int rowIndex = 0; rowIndex < 4; rowIndex++)

 

{

newRow = dtUserDetails.NewRow();

newRow[

"ID"] = rowIndex;

 

 

if (rowIndex == 0)

 

{

newRow[

"Name"] = "A";

 

}

 

else if (rowIndex == 1)

 

{

newRow[

"Name"] = "B";

 

}

 

else if (rowIndex == 2)

 

{

newRow[

"Name"] = "C";

 

}

 

else if (rowIndex == 3)

 

{

newRow[

"Name"] = "D";

 

}

 

else if (rowIndex == 4)

 

{

newRow[

"Name"] = "E";

 

}

 

else if (rowIndex == 5)

 

{

newRow[

"Name"] = "E";

 

}

dtUserDetails.Rows.Add(newRow);

}

dtUserDetails.AcceptChanges();

radGridView1.DataSource = dtUserDetails;

radTextBox1.DataBindings.Add(

"Text", dtUserDetails, "ID" );

 

radTextBox2.DataBindings.Add(

"Text", dtUserDetails, "Name");

 

 

}

 

 

private void radGridView1_SelectionChanged(object sender, EventArgs e)

 

{

 

//radGridView1.EndEdit();

 

 

if (dtUserDetails.GetChanges() != null)

 

{

 

if (MessageBox.Show("Do you want to save the changes", "Save", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)

 

{

 

this.radGridView1.GridElement.BeginUpdate();

 

radGridView1.Relations.Clear();

dtUserDetails.AcceptChanges();

 

this.radGridView1.GridElement.EndUpdate();

 

 

//radGridView1.DataSource = null;

 

 

//radGridView1.DataSource = dtUserDetails;

 

}

}

}

 

private void radGridView1_ValueChanged(object sender, EventArgs e)

 

{

 

//radGridView1.EndEdit();

 

 

}

 

private void radGridView1_CurrentColumnChanged(object sender, CurrentColumnChangedEventArgs e)

 

{

 

}

 

private void radGridView1_RowsChanged(object sender, GridViewCollectionChangedEventArgs e)

 

{

 

if (dtUserDetails.GetChanges() != null)

 

{

 

if (MessageBox.Show("Do you want to save the changes", "Save", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)

 

{

 

this.radGridView1.GridElement.BeginUpdate();

 

dtUserDetails.AcceptChanges();

 

//this.suppliersTableAdapter.Fill(this.nwindDataSet.Suppliers);

 

 

this.radGridView1.GridElement.EndUpdate();

 

 

 

//radGridView1.DataSource = null;

 

 

//radGridView1.DataSource = dtUserDetails;

 

}

}

}

 

}

 

 

public class GridBehavior : Telerik.WinControls.UI.BaseGridBehavior

 

{

 

protected override bool OnMouseDownLeft(MouseEventArgs e)

 

{

 

GridDataCellElement cell = this.GridControl.ElementTree.GetElementAtPoint(e.Location) as GridDataCellElement;

 

 

if (cell != null

 

&& !cell.Equals(

this.GridControl.CurrentCell)

 

&& cell.RowInfo.Equals(

this.GridControl.CurrentRow)

 

&& cell.ColumnInfo.ReadOnly)

{

 

this.GridControl.EndEdit();

 

}

 

try

 

{

 

return base.OnMouseDownLeft(e);

 

}

 

catch (Exception ex)

 

{

 

MessageBox.Show(ex.Message);

 

 

return true;

 

}

}

}

}

3 Answers, 1 is accepted

Sort by
0
Boryana
Telerik team
answered on 17 Jun 2010, 04:26 PM
Hi Guhan Swaminathan,

Thank you for contacting us.

Our team managed to find a solution for your issue. Please note that our First Look example implements a similar behavior so you might find the code of this example useful.

Please have a look at the following snippet:

public partial class Form1 : RadForm
{
DataTable dtUserDetails = new DataTable(); 
public Form1()
{
    InitializeComponent();
}
  
private void Form1_Load(object sender, EventArgs e)
{
    dtUserDetails.Columns.Add("ID"); 
    dtUserDetails.Columns.Add("Name"); 
  
    DataRow newRow; 
  
    for (int rowIndex = 0; rowIndex < 4; rowIndex++) 
    {
        newRow = dtUserDetails.NewRow();
        newRow["ID"] = rowIndex; 
        if (rowIndex == 0)      {  newRow["Name"] = "A"; }
        else if (rowIndex == 1) {  newRow["Name"] = "B"; }
        else if (rowIndex == 2) {  newRow["Name"] = "C"; }
        else if (rowIndex == 3) {  newRow["Name"] = "D"; }
        else if (rowIndex == 4) {  newRow["Name"] = "E"; }
        else if (rowIndex == 5) {  newRow["Name"] = "E"; }
  
        dtUserDetails.Rows.Add(newRow); 
    }
  
    dtUserDetails.AcceptChanges(); 
    radGridView1.DataSource = dtUserDetails;
    radTextBox1.DataBindings.Add("Text", dtUserDetails, "ID" ); 
    radTextBox2.DataBindings.Add("Text", dtUserDetails, "Name");
  
    radTextBox1.TextChanged += new EventHandler(radTextBox1_TextChanged);
    radTextBox2.TextChanged += new EventHandler(radTextBox1_TextChanged);
  
    radGridView1.CellValueChanged += new GridViewCellEventHandler(radGridView1_CellValueChanged);
}
  
bool isModified;
object[] cellValues = new object[2];
  
void radGridView1_CellValueChanged(object sender, GridViewCellEventArgs e)
{
    isModified = true;    
}
  
void radTextBox1_TextChanged(object sender, EventArgs e)
{
    CurrencyManager cm = (CurrencyManager)this.radGridView1.BindingContext[this.radGridView1.DataSource];
    cm.EndCurrentEdit();
    this.radGridView1.CurrentRow.InvalidateRow();
    isModified = true;
}
  
private void radGridView1_RowValidating(object sender, RowValidatingEventArgs e)
{
    if (isModified)
    {
        if (MessageBox.Show("Do you want to save the changes", "Save", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
        {
            e.Cancel = true;
            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                e.Row.Cells[i].Value = cellValues[i];
            }
            isModified = false;
        }
    }
}
  
private void radGridView1_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
{
    for (int i = 0; i < e.CurrentRow.Cells.Count; i++)
    {
        try { cellValues[i] = e.CurrentRow.Cells[i].Value; }
        catch (Exception ex)
        {
            this.radButton1.Text = ex.Message;
        }
    }
    isModified = false;
}
}

I hope you find this helpful. Let me know if I can assist you further.

All the best,
Boryana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Guhan Swaminathan
Top achievements
Rank 1
answered on 25 Jun 2010, 03:21 PM

Thanks for the Reply.
In our application we are using DataBinding Concept. We are binded text box , radgrid with DataTable .Any changes in the textbox when user selects the different row of the grid, application will ask the prompt message like Save or Cancel. If user clicks the "Save" then it will call AcceptChanges() of datatable method , during this time we are getting an exception during that time the application getting closed.  In your solution no where CurrentRowChanging event used the DataAcceptChanges() method called, if its called , it will throw the same exception .Please help me to solve the issue.

Thanks.
Guhan.S


 

 

 

0
Boryana
Telerik team
answered on 01 Jul 2010, 11:38 AM
Hello Guhan Swaminathan,

Thank you for writing.

The exception you get can be easily explained since AcceptChanges method  changes DataRowState. For more information please refer to the following msdn article. Coming back to your application, there is no need to invoke dataTable.AcceptChanges() since your data table is updated automatically. Even if you insist to call the method, you do not need to call Begin/EndUpdate(). Please have a look at the following snippet:

private void radGridView1_SelectionChanged(object sender, EventArgs e)
{
    if (dtUserDetails.GetChanges() != null)
    {
        if (MessageBox.Show("Do you want to save the changes", "Save", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        {
            ((GridBehavior)radGridView1.GridBehavior).CallAcceptChanges = true;
            //dtUserDetails.AcceptChanges();
        }
    }
}
  
public class GridBehavior : BaseGridBehavior
{
    internal bool CallAcceptChanges;
  
    protected override bool OnMouseDownLeft(MouseEventArgs e)
    {
        GridDataCellElement cell = this.GridControl.ElementTree.GetElementAtPoint(e.Location) as GridDataCellElement;
        if (cell != null && !cell.Equals(this.GridControl.CurrentCell) && 
            cell.RowInfo.Equals(this.GridControl.CurrentRow) && cell.ColumnInfo.ReadOnly)
        {
            this.GridControl.EndEdit();
        }
        bool result = base.OnMouseDownLeft(e);
        if (CallAcceptChanges)
        {
            CallAcceptChanges = false;
            ((DataTable)GridControl.DataSource).AcceptChanges();
        }
        return result;
    }
}

Let me know whether the provided solution fits your requirements.

Regards,
Boryana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Guhan Swaminathan
Top achievements
Rank 1
Answers by
Boryana
Telerik team
Guhan Swaminathan
Top achievements
Rank 1
Share this question
or