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

Click event on resizing column

1 Answer 35 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 27 Apr 2011, 06:13 PM
In our application, we have code that needs to execute when the header cell is clicked but needs to be ignored when the column is being resized (by clicking and dragging on the line between header cells).

However, after resizing a column, when the mouse is released, a Click event is created and the code executes.

Is this the correct behavior or an issue?

If the user clicks inside and header cell and drags, no click event occurs when the mouse is released. I assume this is the correct behavior, so why should it happen when resizing columns?

If this is the correct behavior, what is the correct way to excecute code when the user clicks a header cell and not when resizing the column?

Thanks

1 Answer, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 29 Apr 2011, 12:52 PM
Hello Jeff,

There are a couple of ways you could threat this issue, the first one and the easiest one is to register to the SortChanged event and handle everything there, because the sort changed will fire when the header cell has been clicked, but the downside to this is that this event will fire when the user will choose a new sort style from the menu and it won't fire if you disabled sorting.

So, another option is to create your custom grid behavior and use that behavior to check for a column resize. Please take a look at the following example:
First: create the grid behavior like so:
public class MyGridBehavior : BaseGridBehavior
{
    public bool IsResizing
    {
        get
        {
            var currentBehavior = GetCurrentRowBehavior() as GridHeaderRowBehavior;
            if (currentBehavior == null)
            {
                return false;
            }
 
            return currentBehavior.ResizingColumn;
        }
    }
}

as you can see here we are just creating a property here that will tell us if a resize operation is being performed on the header row.

Second, you have to register the new behavior and the CellClick event, like so:
radGridView1.GridBehavior = new MyGridBehavior();
radGridView1.CellClick += new GridViewCellEventHandler(radGridView1_CellClick);

And now for the last part, in the CellClick event, you can do the following:
void radGridView1_CellClick(object sender, GridViewCellEventArgs e)
{
    if (e.Row is GridViewTableHeaderRowInfo)
    {
        var myGridBehavior = radGridView1.GridBehavior as MyGridBehavior;
        if (!myGridBehavior.IsResizing)
        {
            MessageBox.Show("Header Cell Clicked");
        }
    }
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

Telerik WinForms MVP
Tags
GridView
Asked by
Jeff
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Share this question
or