How to get the Selected Row/s in a DevExpress.XtraGrid - Peter's Software House

How to get the Selected Row/s in a DevExpress.XtraGrid

This little helper class helps me to get the selected row/s of a GridView when I use the DevExpress.XtraGrid.

Usage is as Follows:

Say I have a Collection of ProductPrice objects, that is bound to a ObjectDataSource and in turn is bound to a GridView (gridViewActive) that is part of a GridControl (gridControl). (NB!! You will have to change the DomainObject to a base class or interface of your choice or remove it to get it to work for you)

All you do to get the selected row is:

ProductItemPrice selectedPrice = GridHelper.GetSelectedRow<GridView, ProductItemPrice>(gridViewActive);

or

ProductItemPrice selectedPrice = GridHelper.GetSelectedRow<GridView, ProductItemPrice>(gridControl);

Here is the GridHelper Code:

    /// <summary>
    /// Helper for DevExpress XtraGrid
    /// </summary>
    public sealed class GridHelper
    {
        /// <summary>
        /// Gets the selected row.
        /// </summary>
        /// <param name="gridView">The grid view.</param>
        /// <returns></returns>
        public static D GetSelectedRow<D>(ColumnView gridView)
            where D : DomainObject
        {
            int[] selectedRows = gridView.GetSelectedRows();
            if (selectedRows.Length != 1)
                return null;
            D detail = gridView.GetRow(selectedRows[0]) as D;
            if (detail == null)
                return null;
            return detail;
        }
        /// <summary>
        /// Gets the selected row.
        /// </summary>
        /// <param name="gridControl">The grid control.</param>
        /// <returns></returns>
        public static D GetSelectedRow<D>(GridControl gridControl)
            where D : DomainObject
        {
            ColumnView gridView = gridControl.GetViewAt(gridControl.PointToClient(Control.MousePosition)) as ColumnView;
            if (gridView == null)
                return null;
            return GetSelectedRow<D>(gridView);
        }
        /// <summary>
        /// Gets the selected rows.
        /// </summary>
        /// <param name="gridView">The grid view.</param>
        /// <returns></returns>
        public static L GetSelectedRows<L, D>(ColumnView gridView)
            where L : IList, new()
            where D : DomainObject
        {
            L list = new L();
            int[] selectedRows = gridView.GetSelectedRows();
            if (selectedRows.Length == 0)
                return list;
            foreach (int rowIndex in selectedRows)
            {
                D detail = gridView.GetRow(selectedRows[rowIndex]) as D;
                if (detail == null)
                    continue;
                list.Add(detail);
            }
            return list;
        }
        /// <summary>
        /// Gets the selected rows.
        /// </summary>
        /// <param name="gridControl">The grid control.</param>
        /// <returns></returns>
        public static L GetSelectedRows<L, D>(GridControl gridControl)
            where L : IList, new()
            where D : DomainObject
        {
            ColumnView gridView = gridControl.GetViewAt(gridControl.PointToClient(Control.MousePosition)) as ColumnView;
            if (gridView == null)
                return new L();
            return GetSelectedRows<L, D>(gridView);
        }
    }
Published Tuesday, October 31, 2006 3:13 PM by Pieter
Filed under:

Comments

# re: How to get the Selected Row/s in a DevExpress.XtraGrid

what is DomainObject I converted the helper class to vb.net and I get a an undeclared error on this object. Do i need to import some reference or something?

Wednesday, April 02, 2008 10:21 PM by Travis

# re: How to get the Selected Row/s in a DevExpress.XtraGrid

Hey Travis, the DomainObject should not be there, left over from a project I worked on.

Wednesday, May 28, 2008 9:20 AM by Pieter

# re: How to get the Selected Row/s in a DevExpress.XtraGrid

Travis, you should read the post bud: (NB!! You will have to change the DomainObject to a base class or interface of your choice or remove it to get it to work for you)

Wednesday, May 28, 2008 9:25 AM by Pieter

# re: How to get the Selected Row/s in a DevExpress.XtraGrid

This is very useful and worked perfect for me. Now I'm going to extend your helper with some more methods also to implement the SelectItem that doesn't exist in the GridView of devexpress.

The problem is that the only method available is SelectRow(int) and I work with entities ... so is not easy to match the correct index of my custom list ... ;)

Thursday, March 12, 2009 9:25 PM by raffaeu

# re: How to get the Selected Row/s in a DevExpress.XtraGrid

There is an problem in of the functions I think:

public static L GetSelectedRows<L, D>(ColumnView gridView)

D detail = gridView.GetRow(selectedRows[rowIndex]) as D;

should be

D detail = gridView.GetRow(rowIndex) as D;

Friday, April 24, 2009 3:21 PM by Alfred

# re: How to get the Selected Row/s in a DevExpress.XtraGrid

Hi,

Your helper is very useful ! Thanks !

I found a little bug in the method :

public static L GetSelectedRows<L, D>(ColumnView gridView)

the line

D detail = gridView.GetRow(selectedRows[rowIndex]) as D;

must be replace by

D detail = gridView.GetRow(rowIndex) as D;

Am I correct ?

(I use dev express 8.3)

Hope it helps.

Monday, July 13, 2009 4:45 PM by Nigo

# re: How to get the Selected Row/s in a DevExpress.XtraGrid

Is it possible to change DomainObject to an Ilist ?

Monday, September 14, 2009 2:34 PM by u2envy1

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: