This is a migrated thread and some comments may be shown as answers.

Select rows only in master template

10 Answers 226 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Goran
Top achievements
Rank 1
Goran asked on 29 Aug 2010, 01:16 AM
I have a hierarchical grid, but I need to allow users only to be able to select rows in the top level (not sub-levels).
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

Sort by
0
Svett
Telerik team
answered on 30 Aug 2010, 09:29 PM
Hello Gorann,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Goran
Top achievements
Rank 1
answered on 30 Aug 2010, 09:39 PM
Thanks,
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.
0
Svett
Telerik team
answered on 31 Aug 2010, 11:59 AM
Hi Gorann,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Goran
Top achievements
Rank 1
answered on 31 Aug 2010, 04:45 PM
Unfortunately my version (Q1 2010 SP1) doesn't have "Parent" property for GridViewRowInfo class. But I found a way to walk down all rows and select the correct one based on the unique relationship value -- kind of complicated but appears to work. Let me know if there is a more elegant solution.

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?

0
Goran
Top achievements
Rank 1
answered on 01 Sep 2010, 07:47 PM
This solution partially works, but I still have two issues:

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.
0
Svett
Telerik team
answered on 03 Sep 2010, 12:37 PM
Hello Gorann,

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();

All the best,
Svett
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Goran
Top achievements
Rank 1
answered on 03 Sep 2010, 06:59 PM
This solution didn't work!
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.
0
Svett
Telerik team
answered on 07 Sep 2010, 12:14 PM
Hi Gorann,

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;
        }
    }
}

Best wishes,
Svett
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Goran
Top achievements
Rank 1
answered on 08 Sep 2010, 01:09 AM
Yeah, of course it doesn't crash now, but it doesn't do a thing, precisely because the SetCurretRow method doesn't run.

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.
0
Svett
Telerik team
answered on 10 Sep 2010, 05:55 PM
Hello Gorann,

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.

Regards,
Svett
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Goran
Top achievements
Rank 1
Answers by
Svett
Telerik team
Goran
Top achievements
Rank 1
Share this question
or