grid SelectionChanged fired twice
Hi, I encountered a problem using SelectionChanged events.
My project need to know the selected new row but SelectionChanged event will fire twice whenever new row is selected.
I noticed:
First fire - it let me know the prvious selected row
Second fire - it let me know the currect selected row..
it is same for SelectionChanging event too
following is the test app code:
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;
using Dapfor.Net.Data;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.grid1.SelectionChanged += new EventHandler<Dapfor.Net.Ui.GridRowEventArgs>(grid1_SelectionChanged);
for (int i = 0; i < 5; i++)
{
this.grid1.Rows.Add(new dataOBJ());
}
}
private void grid1_SelectionChanged(object sender, Dapfor.Net.Ui.GridRowEventArgs e)
{
dataOBJ temp = (dataOBJ)e.Row.DataObject;
MessageBox.Show(e.Row.VisibleIndex.ToString());
}
}
public class dataOBJ
{
private int colA = new Random().Next(5, DateTime.Now.Millisecond);
private int colB = new Random().Next(2, DateTime.Now.Millisecond);
[Field("Column 0")]
public int ColA
{
get { return colA; }
set { colA = value; }
}
[Field("Column 1")]
public int ColB
{
get { return colB; }
set { colB = value; }
}
}
}
is this by intention or is a bug?
-
Hello,
We especially added selection changing information per item. In this case you know what exactly was changed. If we understand your needs, you have a grid in your application and when the user changes selection you handle this event and perform some actions, taking information from the selected row. Also you need to be notified only once. In this case it will be better to subscribe for Grid. FocusedRowChanged event. The Grid can have only one focused row and you will get this notification only once:
grid.FocusedRowChanged += delegate(object sender, FocusedRowEventArgs e)
{
MessageBox.Show(string.Format("New = {0}, Prev = {1}",
e.NewFocusedRow != null ? e.NewFocusedRow.VisibleIndex.ToString() : "",
e.PrevFocusedRow != null ? e.PrevFocusedRow.VisibleIndex.ToString() : ""));
};
Best regards,
The Dapfor Team0 -
Yes, absolutely right about my needs... thanks for the solution, appreciate it
0
Please sign in to leave a comment.
Comments
2 comments