I tried to detect when the selection is in the sub-level and deselect that row and select corresponding parent row, but it didn't work out.
Is it possible in general to disallow row selection in a grid?
I am using Q1 2010 SP1.
Suggestions? Thanks.
10 Answers, 1 is accepted
You can achieve that by allowing only a row that belongs to MasterGridViewTemplate to be marked as current. You can do it in the CurrentRowChanging event:
private
void
radGridView_CurrentRowChanging(
object
sender, CurrentRowChangingEventArgs e)
{
if
(e.NewRow ==
null
)
{
return
;
}
e.Cancel = !(e.NewRow.ViewTemplate
is
MasterGridViewTemplate);
}
Greetings,
Svett
the Telerik team

I still have to try it, but it looks like the code will prevent selection changes if a user selects a row from a child template. This is great, but it does only a half of what I need. The other half is to select the parent row when a user clicks on a child row. I will try to add code to select the parent row, but if you have something ready to share that would be great.
You can extend the code snippet from my previous post as it is shown below:
private
void
radGridView_CurrentRowChanging(
object
sender, CurrentRowChangingEventArgs e)
{
if
(e.NewRow ==
null
|| e.NewRow.ViewTemplate
is
MasterGridViewTemplate)
{
return
;
}
e.Cancel =
true
;
GridViewRowInfo row = e.NewRow.Parent
as
GridViewRowInfo;
if
(row !=
null
)
{
e.CurrentRow.IsCurrent =
false
;
e.CurrentRow.IsSelected =
false
;
row.IsCurrent =
true
;
row.IsSelected =
true
;
}
}
I hope this helps.
Kind regards,
Svett
the Telerik team

I tried to upgrade to the latest version once before, but the whole grid got messed up so badly that I abandoned the effort for now. Are there any tips or suggestions for upgrading, without starting the design for scratch?

1. When I click on the child table header row, previous row selection disappears, but "CurrentRowChanging" event doesn't fire. This brings the grid into an invalid state for me: no row selection exists. Strangely, when I click on the parent header row, selection doesn't change (although "CurrentRowChanging" doesn't fire either).
2. In the "CurrentRowChanging" event handling I did as you suggested:
row.IsSelected = true;
row.IsCurrent = true;
where "row" is the parent row. Everything looks fine, but if the window goes out of focus, when I came back, the row is still Current, but Selection disappeared.
Please help!
Thanks.
There is some issue regarding current row and selection in Q1 2010 SP1 (2010.1 10.409). I highly recommend upgrading to the latest version Q2 2010 SP1 (v806) where many issues have been addressed.
There is no appropriate place to change the current row inside the Current Row Changing events. You need to change the behavior that handles mouse and keyboard input. You can do it by creating a custom grid behavior. You can use the purposed implementation as it is.
If you still do not want to upgrade you can use the following code snippet that works for Q1 2010 SP1:
public
class
MyGridBehavior : BaseGridBehavior
{
protected
override
bool
OnMouseDownLeft(System.Windows.Forms.MouseEventArgs e)
{
GridViewRowInfo row =
this
.GridControl.CurrentRow;
bool
result =
base
.OnMouseDownLeft(e);
this
.SetCurrentRow(row);
return
result;
}
private
void
SetCurrentRow(GridViewRowInfo row)
{
if
(row !=
this
.GridControl.CurrentRow)
{
if
(
this
.GridControl.CurrentRow.ViewInfo.ParentRow !=
null
)
{
this
.GridControl.CurrentRow.ViewInfo.ParentRow.IsCurrent =
true
;
}
}
}
protected
override
bool
ProcessDownKey(System.Windows.Forms.KeyEventArgs keys)
{
GridViewRowInfo row =
this
.GridControl.CurrentRow;
bool
result =
base
.ProcessDownKey(keys);
this
.SetCurrentRow(row);
return
result;
}
protected
override
bool
ProcessUpKey(System.Windows.Forms.KeyEventArgs keys)
{
GridViewRowInfo row =
this
.GridControl.CurrentRow;
bool
result =
base
.ProcessUpKey(keys);
this
.SetCurrentRow(row);
return
result;
}
protected
override
bool
ProcessLeftKey(System.Windows.Forms.KeyEventArgs keys)
{
GridViewRowInfo row =
this
.GridControl.CurrentRow;
bool
result =
base
.ProcessLeftKey(keys);
this
.SetCurrentRow(row);
return
result;
}
protected
override
bool
ProcessRightKey(System.Windows.Forms.KeyEventArgs keys)
{
GridViewRowInfo row =
this
.GridControl.CurrentRow;
bool
result =
base
.ProcessRightKey(keys);
this
.SetCurrentRow(row);
return
result;
}
}
The equivalent of the code snippet above in Q2 2010 v806 is:
public
class
MyGridBehavior : BaseGridBehavior
{
protected
override
bool
OnMouseDownLeft(System.Windows.Forms.MouseEventArgs e)
{
GridViewRowInfo row =
this
.GridControl.CurrentRow;
bool
result =
base
.OnMouseDownLeft(e);
this
.SetCurrentRow(row);
return
result;
}
private
void
SetCurrentRow(GridViewRowInfo row)
{
if
(row !=
this
.GridControl.CurrentRow)
{
GridViewRowInfo parentRow =
this
.GridControl.CurrentRow.Parent
as
GridViewRowInfo;
if
(parentRow !=
null
)
{
parentRow.IsCurrent =
true
;
}
}
}
public
override
bool
ProcessKey(System.Windows.Forms.KeyEventArgs keys)
{
GridViewRowInfo row =
this
.GridControl.CurrentRow;
bool
result =
base
.ProcessKey(keys);
this
.SetCurrentRow(row);
return
result;
}
}
You need to replace the default behavior by using the following code snippet:
this
.radGridView2.GridBehavior =
new
MyGridBehavior();
Svett
the Telerik team

When I click on the column header row, SetCurrentRow gets called alright, but
this.GridControl.CurrentRow is null, throwing a null object exception on this line:
...
if (this.GridControl.CurrentRow.ViewInfo.ParentRow != null)
...
Thanks.
You simply need to change the SetCurrentRow method. There you can handle the case when the current row is NULL:
private
void
SetCurrentRow(GridViewRowInfo row)
{
if
(row !=
this
.GridControl.CurrentRow &&
this
.GridControl.CurrentRow !=
null
)
{
if
(
this
.GridControl.CurrentRow.ViewInfo.ParentRow !=
null
)
{
this
.GridControl.CurrentRow.ViewInfo.ParentRow.IsCurrent =
true
;
}
}
}
Svett
the Telerik team

The whole trouble is that when you click on the child's row column headers, the current parent selection disappears, and SetCurrentRow method is called, but CurrentRow property is null, and thus no selection happens. Another words, all this new code doesn't really change anything. I am back to the original problem.
However, I did find a work-around, which I hope it will work. I haven't had a chance to exhaustively test it yet.
private void SetCurrentRow(GridViewRowInfo row)
{
if (row != null)
{
if (this.GridControl.CurrentRow == null) // my addition
{
_grid.SelectRows(new string[] { row.Cells[0].Value.ToString() });
}
else if (row != this.GridControl.CurrentRow) // like before
{
if (this.GridControl.CurrentRow.ViewInfo.ParentRow != null)
{
this.GridControl.CurrentRow.ViewInfo.ParentRow.IsCurrent = true;
}
}
}
}
where _grid.SelectRows is a method I wrote to select rows whose first column values match the string array.
I am glad to hear the you have resolved the issue on your own.
If you have further issues feel free to write us back.
Svett
the Telerik team