Blogs

Paul Kimmel's Blog

Handling focus Row Change in XtraGrid

     

A customer asked me yesterday: “I am frustrated. I looked in the Properties window event tab for the XtraGrid.GridControl and cannot find event that tells me when the focused row has changed?” I said I appreciate your frustration; let me help. So, as not to give him a doh! moment with the easy answer, i used the sample code he sent me, completed it and sent it back with a “its the FocusedRowChanged event, which you can find in the events view of the designer”.

Like the guy that showed me how to change a fan belt—not by removing the radiator and fan as I tried to do at 15—but by loosening the alternator, its all about knowing the answer. From the designer implement the FocusedRowChanged event, grab the FocusedRowHandle from the event argument DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e and ask the grid for the row, passing in the FocusedRowHandle. Listing 1 demonstrates the solution.

There are support center posts about this kind of problem, which you can find by Googling for them, or by going directly to our support center or forums at http://community.devexpress.com/forums/. Of course, if you are frustrated it is always nice to reach out to support—they can often find existing posts quickly—or ping one of the evangelists. (The last choice gives us a reason to sling a little code every day.)

Listing 1: Determining what the focused row is in FocsuedRowChanged event.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FocusedRowDemo
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();

      BindingList<guiListItem> list = new BindingList<guiListItem>();

      for (int i = 0; i < 100; i++)
      {
        guiListItem item = new guiListItem(i, "Description " + i.ToString(), "item" + i.ToString());
        list.Add(item);
      }
      gridControl1.DataSource = list;
    }

    private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
    {
      //here
      guiListItem item = (guiListItem)gridControl1.DefaultView.GetRow(e.FocusedRowHandle);
      MessageBox.Show(item.Description);
    }
  }

  public class guiListItem
  {
    /// <summary>
    /// Initializes a new instance of the guiListItem structure.
    /// </summary>
    /// <param name="key"></param>
    /// <param name="description"></param>
    /// <param name="value"></param>
    public guiListItem(int key, string description, string value)
    {
      Key = key;
      Description = description;
      Value = value;
    }
    public int Key{get; set;}
    public string Description{get; set;}
    public string Value{get; set;}
  }
}

Published Jan 07 2010, 06:22 PM by Paul Kimmel (DevExpress)
Bookmark and Share

Comments

No Comments
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.