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

Crash when add new row

1 Answer 202 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Yuriy Rogach
Top achievements
Rank 1
Yuriy Rogach asked on 04 Nov 2008, 10:30 PM
Hi!,

Have a very simple example, binding custom collection to grid. And have a button, in event handler of it "bindingsource.AddNew();". Problem is when you sort grid, then try to add new row, I got "Object reference not set to an instance of an object." exception.

Here is the code, it is very simple:

public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
        }  
 
        private void Form1_Load(object sender, EventArgs e)  
        {  
            InitEmployeeGrid();  
            InitActions();  
            BindData();  
        }  
 
        private void InitEmployeeGrid()  
        {  
            gridView.MasterGridViewTemplate.AllowAddNewRow = false;  
            gridView.MasterGridViewTemplate.AutoGenerateColumns = false;  
 
            GridViewTextBoxColumn nameColumn = new GridViewTextBoxColumn();  
            nameColumn.FieldName = "Name";  
            nameColumn.HeaderText = "Name";  
            nameColumn.UniqueName = "nameColumn";  
 
            gridView.Columns.Add(nameColumn);  
        }  
 
        public void InitActions()  
        {  
            addToolbarButton.Click += new EventHandler(addToolbarButton_Click);  
            cancelToolbarButton.Click += new EventHandler(cancelToolbarButton_Click);  
        }  
 
        void cancelToolbarButton_Click(object sender, EventArgs e)  
        {  
            if (gridView.DataSource != null)  
            {  
                (gridView.DataSource as BindingSource).CancelEdit();  
                gridView.CancelEdit();  
            }  
        }  
 
        void addToolbarButton_Click(object sender, EventArgs e)  
        {  
            if (gridView.DataSource != null)  
            {  
                (gridView.DataSource as BindingSource).AddNew();  
                gridView.BeginEdit();  
            }  
        }  
 
        private void BindData()  
        {  
            employeeBindingSource.DataSource = CreateDataSource();  
            gridView.DataSource = employeeBindingSource;  
        }  
 
        private object CreateDataSource()  
        {  
            List<Employee> result = new List<Employee>();  
 
            result.Add(new Employee("Georg"));  
            result.Add(new Employee("Mike"));  
            result.Add(new Employee("Timothy"));  
 
            return result;  
        }  
    } 

Can anybody help me with this ? Thanks.

1 Answer, 1 is accepted

Sort by
0
Nick
Telerik team
answered on 07 Nov 2008, 01:41 PM
Hello Yuriy Rogach,

Thank you for your question and sample code.

I spotted the exception. Please add the new object through your collection, because the grid is two-way synchronized with its data source. For example:

result.Add(new Employee("Tom")); 
radGridView1.Rows[this.radGridView1.Rows.Count - 1].Cells[0].BeginEdit(); 
 
When adding or removing data from the grid from code outside it, it is always better to use the methods that your collection/datatable provides. The grid will automatically reflect the underlying changes. This will result in better code. You may also switch to BindingList instead of just a List. This is another good practice though it is not necessary in your case because the add new row through the grid feature is disabled.

Do not hesitate to write me back if you have more questions.
 

Best wishes,
Nick
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
GridView
Asked by
Yuriy Rogach
Top achievements
Rank 1
Answers by
Nick
Telerik team
Share this question
or