开发者

Is there a way to use OnMouseOver to select a radgrid row?

开发者 https://www.devze.com 2023-01-03 10:04 出处:网络
I am currently highlighting a row in a radgrid using OnMouseOver. I would like to know if it 开发者_如何学编程is possible to use OnMouseOver to select the row rather than highlight it.

I am currently highlighting a row in a radgrid using OnMouseOver. I would like to know if it 开发者_如何学编程is possible to use OnMouseOver to select the row rather than highlight it.

Alternatively, I would like the highlighted row to remain highlighted if the radgrid loses focus, such as when a confirmation box pops up.

Thanks in advance.


According to Telerik documentation, it should be possible to select the item OnMouseOver using the following code (if you don't have any detail tables you can nix the if statement and just use the code from the else block to find the currentDataItem):

function RadGrid1_RowMouseOver(sender, eventArgs) {
    var currentDataItem = null;

    // clear all currently selected items before selecting new
    sender.get_masterTableView().clearSelectedItems();

    if (eventArgs.get_itemIndexHierarchical().indexOf(':') > 0)
    {
        var detailTableIndex = eventArgs.get_itemIndexHierarchical().split(':')[0];
        var rowIndex = eventArgs.get_itemIndexHierarchical().split(':')[1].split('_')[1];
        currentDataItem = sender.get_detailTables()[detailTableIndex].get_dataItems()[rowIndex];
    }
    else
    {
        currentDataItem = sender.get_masterTableView().get_dataItems()[eventArgs.get_itemIndexHierarchical()];
    }

    if (currentDataItem != null)
    {
        currentDataItem.set_selected(true);
    }
}


The other answers here do not work with the WPF Telerik RadGridView as we don't have access to RowMouseOver event.

For a WPF Telerik RadGridView, the best approach if the grid doesn't contain UI elements is to use ChildrenOfType<> in a Linq expression with IsMouseOver.

private void myGridView_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
    MyCustomClass myClass = null;

    var rows = this.myGridView.ChildrenOfType<GridViewRow>().Where(r => r.IsMouseOver == true);
    foreach (var row in rows)
    {
        if (row is GridViewNewRow) break;
        GridViewRow gvr = (GridViewRow)row;
        myClass = (MyCustomClass)gvr.Item;
    } 
    // do something with myClass here if we have found a row under mouse
}


Thanks! Your solution worked great, but rows would not become unselected when mousing over another row even if AllowMultiRowSelection was set to False. The following code will select a single row in the radgrid when the mouse hovers over the row:

<script type="text/javascript">

    function grdUsers_RowMouseOver(sender, eventArgs) {

        var NumberItems = sender.get_masterTableView().get_dataItems().length;
        for (var count = 0; count < NumberItems; count++) {
            var currentDataItem = sender.get_masterTableView().get_dataItems()[count];
            if (count == eventArgs.get_itemIndexHierarchical()) {
                currentDataItem.set_selected(true);
            }
            else {
                currentDataItem.set_selected(false);
            }
        }
    } 
</script>

I called the function at the following location:

<ClientSettings AllowColumnsReorder="True" ReorderColumnsOnClient="True">
                    <Selecting AllowRowSelect="True" />
                    <ClientEvents OnRowMouseOver="grdUsers_RowMouseOver" />
                </ClientSettings>
0

精彩评论

暂无评论...
验证码 换一张
取 消