I am trying to insert a new row in a grid and then set the first cell automatically to edit mode. The rows in my grid are inserted via databinding to a business object, and not directly to the grid from a button's click. i.e. a row insert is done by adding a new business object into the datasource and then binding it to the grid.
I tried to set the focus by writing the code in load, paint, button click etc., but not successfull, 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.
I found a similar post(having same problem) in the forums - http://community.infragistics.com/forums/t/7935.aspx?PageIndex=1
None of the ways suggested in above post is usefull for me, like -
this.grid.Focus(); this.grid.Rows.FilterRow.Cells["Name"].Activate(); this.grid.PerformAction(UltraGridAction.FirstCellInRow); this.grid.PerformAction(UltraGridAction.EnterEditMode);or
private void button1_Click(object sender, EventArgs e) {
// Code to add a new object in the collection and do data bind
userCotrol.setFocus();
}
public void setFocus()
{
this.BeginInvoke(new MethodInvoker(this.SetFocusToFirstCell))
private void SetFocusToFirstCell() { System.Threading.Thread.Sleep(200); this.grid.Focus(); this.grid.ActiveCell = this.grid.Rows.FilterRow.Cells["Name"]; this.grid.PerformAction(UltraGridAction.EnterEditMode);
My grid is in a user control and button is on the form, I also checked the cell for it's readonly attribute and that was fine, can any body point out why it's not happening for me?
-Anand
Any hints?
If you are calling this code from a button click event, then you should not need to use BeginInvoke, and you certainly don't need to call thread.sleep. Why are you doing that? I'd take those out and see if it works.
I tried that also, see the below code -
userCotrol.SetFocusToFirstCell();
//Below function is in user control.
public void SetFocusToFirstCell()
this.grid.Focus(); this.grid.ActiveCell = this.grid.Rows.FilterRow.Cells["Name"]; this.grid.PerformAction(UltraGridAction.EnterEditMode);
Even I tried - "this.grid.Rows.FilterRow.Cells["Name"].Activate();", in place "this.grid.ActiveCell=........" statement.
When the first row is created and I click somewhere on the grid(either in column or header), and then during second row creation, it receives the focus and selects the text there. (only when I click the grid before clicking the button to create the second row ).
I don't know why it's not happening with the first call, I even try below code in the button click to make sure that user control has focus but no success-
userCotrol.Focus();
I am not able to figure out where I am wrong, any hint would be great help.
Hi Anand,
This looks like it should work. I can't see anything wrong or missing here.
Can you reproduce this in a simple sample project?
Have you tried setting focus to any other controls inside the UserControl to see if that works? That might be worth trying. At the very least, it will tell us if this is some UserControl issue or something specific to the grid.
Hi Mike,
Sorry for late reply, I need some time to create that sample, because I can't post my actual code in the forum here, will be back soon with the sample project.
Thanks,
Anand