Hi Richard,
To stop navigating to a readonly cell in a DataGridView via Tab, we could
derive a new class from DataGridView and override the ProcessDialogKey and
ProcessDataGridViewKey methods in the subclass.
When the current cell in a DataGridView is in edit mode, and the user press
a key, the override ProcessDialogKey method is called. When the current
cell is not in edit mode, and the user press a key, the override
ProcessDataGridViewKey method is called. So these two methods compensate
each other.
In the overrid ProcessDialogKey and ProcessDataGridViewKey methods, seek
the next editable cell and if find then set it as the current cell.
The following is a sample.
class MyDataGridView:DataGridView
{
protected override bool ProcessDialogKey(Keys keyData)
{
Keys key = (keyData & Keys.KeyCode);
if (key == Keys.Tab)
{
int col = this.CurrentCell.ColumnIndex + 1;
for (; col < this.Columns.Count; col++)
{
if (!this.Columns[col].ReadOnly)
{ break; }
}
if (col < this.Columns.Count)
{
this.CurrentCell =
this.Rows[this.CurrentCell.RowIndex].Cells[col];
}
else
{
if (this.CurrentCell.RowIndex != this.Rows.Count - 1)
{
for (col = 0; col <= this.CurrentCell.ColumnIndex;
col++)
{
if (!this.Columns[col].ReadOnly)
{
break;
}
}
if (col <= this.CurrentCell.ColumnIndex)
{
this.CurrentCell =
this.Rows[this.CurrentCell.RowIndex + 1].Cells[col];
}
}
}
return true;
}
return base.ProcessDialogKey(keyData);
}
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{
if (e.KeyData == Keys.Tab)
{
int col = this.CurrentCell.ColumnIndex +1;
for (; col < this.Columns.Count; col++)
{
if (!this.Columns[col].ReadOnly)
{ break; }
}
if (col < this.Columns.Count)
{
this.CurrentCell =
this.Rows[this.CurrentCell.RowIndex].Cells[col];
}
else
{
if (this.CurrentCell.RowIndex != this.Rows.Count - 1)
{
for (col = 0; col <= this.CurrentCell.ColumnIndex;
col++)
{
if (!this.Columns[col].ReadOnly)
{
break;
}
}
if (col <= this.CurrentCell.ColumnIndex)
{
this.CurrentCell =
this.Rows[this.CurrentCell.RowIndex + 1].Cells[col];
}
}
}
return true;
}
return base.ProcessDataGridViewKey(e);
}
}
Use the derived DataGridView on your form and you should see the readonly
cells don't get selected when you navigate through the DataGridView using
TAB key.
As for your second question of prevent the readonly cells from being
selected via mouse click, I will go on research and will get back to you
ASAP. I appreciate your patience.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.