Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
2430
The Arrow pointing to the row doesn't show up when switching rows programatically
posted

       

I have pretty much a grid (RoationPersonnelGrid) with a single cell that user can add rows to.  If user chooses an existing value and tries to add, I want to avoid duplicates and cancel the row addition and push the user to the existing row.  It works great, except that it doesn't have the Active row indicator showing.  Not sure what I need to do to get the indicator working...

Current code below works, just want it to work a little better...

private void BeforeCellUpdate(object sender, BeforeCellUpdateEventArgs e)

{

  int iNewEmpCode = Convert.ToInt32(e.NewValue);

  foreach (UltraGridRow row in RotationPersonnelGrid.Rows)

    {

       if (!(row.Cells["EMP_CODE"].Value is DBNull) && Convert.ToInt32(row.Cells["EMP_CODE"].Value) == iNewEmpCode)

                {

                    RotationPersonnelGrid.Selected.Rows.Clear();

                    RotationPersonnelGrid.Selected.Rows.Add(e.Cell.Row);

                    RotationPersonnelGrid.DeleteSelectedRows(false);

                    RotationPersonnelGrid.Selected.Rows.Clear();

                    RotationPersonnelGrid.Selected.Rows.Add(row);

                    RotationPersonnelGrid.ActiveRow = row;

                     e.Cancel = true;

          return;

                }

            }

        }

  • 469350
    Verified Answer
    Offline posted

    Hi,

    The arrow is a product of the ActiveRow. Selection and the selected row(s) has no bearing.

    So what's probably happening here is this line of code is failing:

    RotationPersonnelGrid.ActiveRow = row;

    I don't think you can do this inside of BeforeCellUpdate. The grid is in the middle of the process of updating a cell in the current row and moving to another row would cause the current row to be updated again. So this line of code is most likely being ignored.

    What you would have to do is wait for the current update operation to complete and then set the ActiveRow. Normally, I would say set a flag inside this event and then use some later event like AfterRowUpdate to set the ActiveRow. But since you are cancelling the cell update here, you probably won't even get an AfterRowUpdate. And there's no event for after the cell/row update is cancelled.

    So maybe you could try introducing a delay using BeginInvoke. I tried this out in a very simple proof-of-concept scenario and it seems to work for me.


            void ultraGrid1_BeforeCellUpdate(object sender, BeforeCellUpdateEventArgs e)
            {
                if (e.Cell.Text == "A")
                {
                    e.Cancel = true;

                    UltraGridRow row = this.ultraGrid1.Rows[0];
                    
                    // This does not work.
                    //this.ultraGrid1.ActiveRow = row;

                    // This works
                    this.ultraGrid1.BeginInvoke(new RowDelegate(this.SetActiveRow), new object[] { row });
                }
            }

            private delegate void RowDelegate(UltraGridRow row);

            private void SetActiveRow(UltraGridRow row)
            {
                this.ultraGrid1.ActiveRow = row;
            }