This project is read-only.

Cell Controller vs. Grid controller

Jul 29, 2009 at 3:24 AM

I am trying to add a controller to a cell only and I'm having problems. I get the following error when I try to add the controller: "Object reference not set to an instance of an object."  The title of the error was Null Reference Exception Was Unhadled.

When I add the controller to the entire grid, it works fine. Any Ideas? Below is the 2 lines of code and the controller class. Any help would be appreciated.


grid1[r, 0].Controller.AddController(new g1ValueChangedEvent());   <<<----- This gives me the error

// grid1.Controller.AddController(new g1ValueChangedEvent());      <<<----- This Works




public class g1ValueChangedEvent : SourceGrid.Cells.Controllers.ControllerBase
        public override void OnValueChanged(SourceGrid.CellContext sender, EventArgs e)
            base.OnValueChanged(sender, e);
            int c = sender.Position.Column;
            int r = sender.Position.Row;
            string NewTag = sender.DisplayText;
            bool IsTag = false;
            if (NewTag != null && c == 0)
                IsTag = Form1.IsValidTagStructure(NewTag);
                if (IsTag)
                    Form1.g1_Add_Item(NewTag, r, c);

Jul 29, 2009 at 4:50 PM
is grid[r,0] initialized? eg, have you done a new Cell() on it ? that is probably the problem ...
Jul 30, 2009 at 12:28 AM

Below is the code for when i add a row to the grid, so yes the cell is initialized. I got it to work using the following line:

grid1[r, 0].AddController(g1Controller);  // This works.

but the documentation shows that:       grid1[r,0].Controller.AddController(g1Controller);   should work.


I don't know but it works now, so i can have different controllers for each of my cells.




private void g1AddRow_Click(object sender, EventArgs e)
            int r = grid1.Rows.Count;
            if (r < 31)
                grid1[r, 0] = new SourceGrid.Cells.Cell("", typeof(string));
                grid1[r, 1] = new SourceGrid.Cells.Cell(1, typeof(bool));
                grid1[r, 1].Editor.EnableEdit = false;

                grid1[r, 2] = new SourceGrid.Cells.Cell("Enter Description", typeof(string));
                grid1[r, 2].Editor.EnableEdit = true;

                grid1[r, 3] = new SourceGrid.Cells.Cell(null, typeof(int));
                grid1[r, 3].Editor.EnableEdit = false;

                SourceGrid.Cells.Views.Cell OddRowView = new SourceGrid.Cells.Views.Cell();
                OddRowView.BackColor = Color.FromArgb(215,245,185);
                OddRowView.TextAlignment = DevAge.Drawing.ContentAlignment.BottomLeft;

                SourceGrid.Cells.Views.Cell EvenRowView = new SourceGrid.Cells.Views.Cell();
                EvenRowView.BackColor = Color.FromArgb(200, 245, 240);
                EvenRowView.TextAlignment = DevAge.Drawing.ContentAlignment.BottomLeft;

                if (r % 2 == 0)
                    grid1[r, 0].View = EvenRowView;
                    grid1[r, 1].View = EvenRowView;
                    grid1[r, 2].View = EvenRowView;
                    grid1[r, 3].View = EvenRowView;
                    grid1[r, 0].View = OddRowView;
                    grid1[r, 1].View = OddRowView;
                    grid1[r, 2].View = OddRowView;
                    grid1[r, 3].View = OddRowView;

                ValueChangedEvent g1Controller = new ValueChangedEvent();
                //grid1[r, 0].Controller.AddController(g1Controller);   //<<<----- This gives me the error

                grid1[r, 0].AddController(g1Controller);  // This works.