Home Forums WinForms controls Xceed Grid for WinForms Trouble with currentcellchanged event

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #13534 |

    We are evaluating Xceed grid for use in our applications. So far it seems like a great component with a well thought out UI and object-model, and good documentation/support. Sorry, if my question is newbie stuff…

    I am having trouble using the currentCellChanged event. I want to use it to set the back color of the “current cell” and set the row height according to the “fitted height” of the current cell. I want the former current cell/row to revert when the current cell changes. Example code is found below. When I run that code I get an exception when EndInit() is called.

    The exception says “Exception has been thrown by the target of an invocation.” with the inner-exception being “An attempt was made to bind the grid to a data source while in an invalid state.” It works fine without the line that adds the event handler.

    Could you tell me what I am doing wrong? I have seem to have problems no matter where I try to set that event (prior to data bind, after, or even in the VS.Net designer). Thanks for your help!

    Nick

    PS: I have made a similar behavior work using cell click events, but that does not cover all cases where the cell changes. In that case I am working with a DataCell. What exactly is the difference between a Xceed.Grid.Cell and a Xceed.Grid.DataCell?

    ——————————————————————————————————-

    private Xceed.Grid.Cell currentCell = null;

    private LoadSomeData(DataTable T)
    {
    gridControl1.BeginInit();
    gridControl1.DataSource = T;
    currentCell = null;
    gridControl1.CurrentCellChanged +=new EventHandler(gridControl1_CurrentCellChanged);
    gridControl1.EndInit();
    }

    private void gridControl1_CurrentCellChanged(object sender, EventArgs e)
    {
    int maxHeight = 100;
    Xceed.Grid.Cell newCell = gridControl1.CurrentCell;
    newCell.BackColor = Color.LemonChiffon;
    newCell.ParentRow.Height = Math.Min(maxHeight,newCell.GetFittedHeight(Xceed.Grid.AutoHeightMode.AllContent));
    if (currentCell != null)
    {
    currentCell.BackColor = currentCell.AmbientParent.BackColor;
    currentCell.ParentRow.Height = currentCell.GetFittedHeight(Xceed.Grid.AutoHeightMode.Minimum);
    }
    currentCell = newCell;
    }

    Imported from legacy forums. Posted by nvfryslan42 (had 994 views)

    User (Old forums)
    Member
    Post count: 23064

    Try this: keep a boolean variable initializingGrid (like your currentCell variable), and set it to false.
    In LoadSomeData, you set the variable to true, before BeginInit, and set it to false after EndInit.
    In gridControl1_CurrentCellChanged, you check the variable, and exit the method when the variable is true.

    private Xceed.Grid.Cell currentCell = null;
    private bool initializingGrid = false;

    private LoadSomeData(DataTable T)
    {
    initializingGrid = true;
    gridControl1.BeginInit();
    gridControl1.DataSource = T;
    currentCell = null;
    gridControl1.CurrentCellChanged +=new EventHandler(gridControl1_CurrentCellChanged);
    gridControl1.EndInit();
    initializingGrid = false;
    }

    private void gridControl1_CurrentCellChanged(object sender, EventArgs e)
    {
    if (initializingGrid) return;
    int maxHeight = 100;
    Xceed.Grid.Cell newCell = gridControl1.CurrentCell;
    newCell.BackColor = Color.LemonChiffon;
    newCell.ParentRow.Height = Math.Min(maxHeight,newCell.GetFittedHeight(Xceed.Grid.AutoHeightMode.AllContent));
    if (currentCell != null)
    {
    currentCell.BackColor = currentCell.AmbientParent.BackColor;
    currentCell.ParentRow.Height = currentCell.GetFittedHeight(Xceed.Grid.AutoHeightMode.Minimum);
    }
    currentCell = newCell;
    }

    Imported from legacy forums. Posted by Tommy (had 3292 views)

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.