Home - Forums-.NET - FlyGrid.Net (Windows Forms) - How to programmatically set focus on cell for edit

FlyGrid.Net (Windows Forms)

.NET Datagrid - Fast, highly customizable, industry standards .NET data grid control for WinForms

This forum related to following products: FlyGrid.Net

How to programmatically set focus on cell for edit
Link Posted: 13-Jun-2006 04:08
Hi,

I want to programmatically set the focus on a cell so it can be typed in. I tried this.FocusNode(this.ActiveRootPort, newNode, ColumnCache[\"symbol\"], false); However, this doesn't do it?

Thanks,

Tom
Link Posted: 13-Jun-2006 13:49
[c#]
public class AdvFlyGrid : FlyGrid
{
  public virtual void SelectCell(NodeBase node, Column col)
  {
    if (node != null && col != null)
    {
      int colIndex = this.Columns.GetColumnCellIndex(col);
      if (colIndex != -1)
      {
        this.FocusNode(this.ActiveRootPort, node, colIndex, true);
        this.Selected = node;
      }
    }
  }
}


usage:
[c#]
//this handler gets current selected node, selects next and focus
//cell on second column
private void button2_Click(object sender, System.EventArgs e)
{
  NodeBase node = virtualGrid.Selected;
  NodeBase next = node.GetNext() as NodeBase;
  //if next node is null (selected node is last node), we get first
  if (next == null)
    next = node.Owner.GetFirst() as NodeBase;
  if (next != null)
  {
    virtualGrid.SelectCell(next, virtualGrid.Columns.Items[1]);
  }
}
Link Posted: 15-Jun-2006 21:18
this doesn't seem to work when rows are sorted with a node that is always sorted to the bottom. Additionally, what happens is that two or even more cells of that first column now have the selected cell rectangle drawn.
Link Posted: 16-Jun-2006 01:01
I've tried more advanced test (focus cell in a current row, if last cell in row is focused - select next row):
[c#]
private int curColumn = -1;//start position
private void SelectNext(FlyGrid flyGrid)
{
  NodeBase node = flyGrid.Selected;
  bool selectNextNode = false;

  if (curColumn == -1)
    curColumn = 0;
  else
  {
    curColumn++;
    if (curColumn >= flyGrid.Columns.VisibleColumns.Length)
    {
      curColumn = 0;
      selectNextNode = true;
    }
  }

  NodeBase next = node != null ?
    selectNextNode ? node.GetNext() as NodeBase : node :
    flyGrid.Rows.RootNode.GetFirst() as NodeBase;
  
  //if next node is null (selected node is last node), we get first
  if (next == null)
    next = node.Owner.GetFirst() as NodeBase;

  if (next != null)
  {
    flyGrid.SelectCell(next, flyGrid.Columns.VisibleColumns[curColumn]);
  }
}

But this test is works correctly.

Probably you incorrectly determine focused column in custom drawing,
use info.grid.Columns.CurrentColumn - this is an index of focused column in info.grid.Columns.VisibleColumns array.
Link Posted: 16-Jun-2006 01:02
BTW, SelectCell method will included into FlyGrid methods with the nearest update.
Link Posted: 16-Jun-2006 01:14
Victor, honestly it's very frustrating that you're ALWAYS assuming I'm the one doing something wrong until I have to prove that there is a flygrid bug by going through the extra hassle and time spent to write up examples, etc.

I'm absolutely not using the wrong column index to determine the focused column:
Columns cols = info.grid.Columns;
if (this.Selected && cols.CurrentColumn == info.col) {...code...}

Also, if you read my description of the problem again, it's not that two cells in different columns draw the selected rectangle, but two cells in the same column. I've been working with flygrid for quite some time now and I think I have a pretty good idea of what I'm doing.

So, what I wrote as well in my last post is that it doesn't work
when rows are sorted with a node that is always sorted to the bottom.
It works when rows are not sorted but as soon as sorting comes into play it doesn't work anymore.

Tom
Link Posted: 16-Jun-2006 01:28
btw. the multiple active rectangles happen whenever I call:
this.FocusNode(this.ActiveRootPort, newNode, ColumnCache[\"symbol\"], true);
newNode.Selected = true;
Link Posted: 16-Jun-2006 01:42
Actually it doesn't even work correctly when no sorting is used. I once again wrote up a sample that shows this behavior:
http://www.kingvest.de/bugs/WindowsApplication8.rar

Start the application, enter '2' in the first cell, hit enter. See how, new node is added with correct focus but the \"old\" cell has active recttangle. Now enter a '3' in the current active cell (don't click anywhere!)... now two rectangles are drawn, etc...

Now to show the incorrect behavior of FocusNode, close the application and start it again. Enter a '12' (yes 12) in the first cell and hit enter .. see what happens ... new node has no focus and two rects are drawn
Link Posted: 16-Jun-2006 02:17
Thanks for the sample.
Sorry, but I didn't know about context of usage - next time please provide a sample, where I can see the usage.
Also see this topic - may be this topic related to your problem.
Link Posted: 16-Jun-2006 02:23
hm, it's odd .. I just tried: this.ActiveRootPort.BeginEdit(newNode.Index, 0); instead..
it works correct when I enter 2 .. then 3... etc. and also the rects are drawn correctly. However, when I enter 12 it breaks again with the cell not being in edit state.