I try to insert a new row in a grid and then set a cell automatically to edit mode. The row is inserted via databinding to a business object, and not directly to the grid. The row is highlighted but no cell is in edit mode. If I first click on the grid and then inserts a new row it works for every new row thereafter. It's look like I am missing something the mouseclick do. A row insert is done by adding a new business object, and I deal with the new row in InitializeRow like this:
{
e.Row.Activate();
grid.ActiveCell = aCell;
}
The PerformAction seems to have no effect. If I click the grid first, it works as expected.
Some suggestion?
Kind Regards
Magnus
Oslo, Norway
It seems to me that you should be setting focus to the cell using the click of the button. You don't need a grid event for this.
private void button1_Click(object sender, EventArgs e) { DataRow row = this.dt1.NewRow(); this.dt1.Rows.Add(row); this.ultraGrid1.Rows[this.ultraGrid1.Rows.Count - 1].Cells[0].Activate(); this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode); this.ultraGrid1.ActiveCell.SelStart = 0; }
It should have work. But I get an error on selstart ( not supported while cell is not in edit mode). Maybe it has something to do with the databinding? I got no error if I mouse click the grid first.
BTW: AfterRowInsert is not fired in my case, but a row is in fact inserted.
I have tried to change the code and moved all the row insert into the click event for the "insert new row button".
// Insert a new object. The grid will then automatically insert a new row via bindingsource
// Norwegian word is not easy to read for you but it is something like this if the grid is databind to objectcollection called BOCollection:
// BOCollection.Add(New BOItem(Id));
// Jump to the last newly created row. This PerformAction work fine.
// Set the column "Tekst" in the active row as activ cell.
csfGridcsfIkkeBehandletNemd.ActiveCell = cell;
// Try to set active cell in editmode. This does not work. No errormessage
// The next statement will trigger an error message "... are not supported while cell is not in editmode)
cell.SelStart = 0;
How about using a method like this:
private void YourUltraGrid_AfterRowInsert(object sender, Infragistics.Win.UltraWinGrid.RowEventArgs e) { YourUltraGrid.ActiveRow = e.Row; YourUltraGrid.ActiveCell = e.Row.Cells["FirstColumnName"]; YourUltraGrid.PerformAction(UltraGridAction.EnterEditMode, false, false); YourUltraGrid.ActiveCell.SelStart = 0; }
This uses the AfterRowInsert of the grid, and worked perfectly when I tested it on a project this morning. Good luck!
But I don't understand why this code never will set the active cell in edit mode. It works only if the user first mouseclick the grid.
An observation, the event AfterRowInsert is not fired when a row is inserted via databinding.
Thanks for all answer
It's quit simple task I want do. I want the user to push a add button,a new row is added to the grid and ready for typing without an extra mouseclick to enter edit mode.
Intstead of doing an add directly to the grid, I try to add a new element to the underlying datasource. This work fine, a new row is inserted via the bindingsource. I just add a new object to the collection of objects the grid is databind to, and some magic do the insert to the grid.
Then I want to use a event to do EnterEditMode for the first visble cell in this newly added row. I understand InitializeRow is not the best event to use, so I must try to find another event.
The object I use in the databinding to this grid is in the framework of CSLA, and they have no public constructor by design, but only factory methods. Therfore I get an error if a use the AddButton included in the grid. (No constructor found)