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:
Can anybody help me with this ? Thanks.
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.