Mark,
The problem still seems to be occuring even when the first column is not hidden, and after I tried overriding SetCurrentCellAddressCore(), as you suggested. Here's some test code illustrating how to reproduce the exception.
Is it still a bug, or just something I am doing wrong in my experiment, please?
Thank you.
Steps:
- Create a standard WindowsApplication, call it "SetCurrentCellAddressCore".
- Add a new DataSet item to the project (that'll be our DataSet1 type).
- Add three tables with two columns each (accept default names for both table and column names) to the DataSet (using the DataSet editor, in DataSet1.xsd).
- Place a TreeView and a DataGridView on the form.
- Paste the code below into the Form1.cs file (within the namespace brackets).
- Compile and run the app.
- Click on Node1.
- Click on the cell displaying text “DataTable1�
- Press “Enter�
- Get the exception.
public partial class Form1 : Form
{
private DataSet1 _dataSet = new DataSet1();
private CurrencyManager _RowManager;
public Form1()
{
InitializeComponent();
treeView1.Nodes[0].Nodes[0].Tag = _dataSet.Tables[0];
treeView1.Nodes[0].Nodes[1].Tag = _dataSet.Tables[1];
treeView1.Nodes[0].Nodes[2].Tag = _dataSet.Tables[2];
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if (e.Node.Tag != null)
{
ChangeTable(e.Node.Tag
as DataTable);
}
}
private void ChangeTable(DataTable newTable)
{
dataGridView1.DataSource = newTable;
if (_RowManager != null)
{
_RowManager.PositionChanged -=
new EventHandler(cursor_PositionChanged);
}
_RowManager = BindingContext[newTable]
as CurrencyManager;
_RowManager.PositionChanged +=
new EventHandler(cursor_PositionChanged);
if (newTable.Rows.Count == 0)
{
newTable.Rows.Add(newTable.TableName, newTable.TableName);
}
}
void cursor_PositionChanged(object sender, EventArgs e)
{
if (-1 == _RowManager.Position)
{
return;
}
if ((_RowManager.Current as DataRowView).IsNew)
{
try
{
//We only want to respond when the user scrolls to the next row in dataGridView.
if (!treeView1.Focused)
{
TreeNode current = treeView1.SelectedNode;
//Select next treeNode (in a wrap-around fashion).
if (current.Parent != null && current.Parent.GetNodeCount(false) > 1)
{
TreeNode sibling = current.NextNode;
if (sibling != null)
{
treeView1.SelectedNode = sibling;
}
else
{
treeView1.SelectedNode = current.Parent.Nodes[0];
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}