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;
}
}
}
}