Windows Develop Bookmark and Share   
 index > Windows Forms Designer > DataGridView SelectionChanged event fired 3 times
 

DataGridView SelectionChanged event fired 3 times

Hi there,I've some problem using datagridview..
So like this...
I've this code:

private void frmOwners_Load(object sender, System.EventArgs e)
{
this.FillGrid();
//after this is done, there are fired 2 more SelectionChanged events
}

private void FillGrid()
{
DataTable myDt = new DataTable();

myDt = DBEngine.GetAllRecords(); //its my own method for getting data...

myDt.Columns[0].Column.ColumnMapping = MappingType.Hidden;
myDt.ColumnsDevil.Column.ColumnMapping = MappingType.Hidden;
myDt.Columns[7].Column.ColumnMapping = MappingType.Hidden;
myDataGrid.DataSource = myDt; //This fires the first selectionChanged method...
}

private void myDataGrid_SelectionChanged(object sender, EventArgs e)
{
//here is some code, but it's fired 3 times
}

The last row from code fires the selectionchanged method, but, it's fired 3times one by one...
Does anyone know why???
Is it because the 3 columnmappings?

THX for any help
metaller

PS: Oh, i forgot... using XPs, VS2005, NET2.0
metaller  Friday, June 01, 2007 8:51 AM

hi metaller

I've tried your code, it works fine. I don't know your last row from code, which line code. Can you provide it exactly.

Thank you.

Guang-Ming Bian - MSFT  Monday, June 04, 2007 2:09 AM
this is exactly the whole code...
this is the last row i've wrote, which fires the event, but it's done 3 times...
myDataGrid.DataSource = myDt; //This fires the first selectionChanged method...

The whole method FillGrid() as you can see, is done before the form (frmOwners) is loaded...
Maybe there is some clue for solving my problem Smile

metaller  Monday, June 04, 2007 7:03 AM

hi metaller

The reason is not columnmappings ,or other reasons, but the datagridview control itself.

if you bind data to datasource, it always fires three times.

Guang-Ming Bian - MSFT  Tuesday, June 05, 2007 4:57 AM

When I assigned the Datasource to DataGridView it fires the selectionChanged 3 times & it selects the first row. But my requirment is not to select anything untill user selects it. Can we avoide Slecting the first row some how b'couse I have lot of code in Selection Changed which slows down the app to come up.

Thanks in adavance

kiru_99  Thursday, June 28, 2007 3:13 PM
The SelectionChanged even will fire everytime the a row gets added.

maybe try adding your SelectionChanged event handler after you have filled the datagrid?

Rick
TheDirtyMexican  Wednesday, July 11, 2007 8:31 AM

I got around this by adding a Private Boolean variable to my Silverlight control with the datagrid like:

Private mblnLoaded As Boolean

Then, right before the call to the MyStoredProcAsync method, I set this variable to False

mblnLoaded = False

'

Call mobjSvcClient.StudentSelectListAsync(Parms...)

Right after the Completed call back is invoked, I set the variable to True.

If Not e.Result Is Nothing Then

If e.Result.Count < 1 Then

dgStudent.ItemsSource = Nothing

Else

dgStudent.ItemsSource = e.Result

End If

End If

'

mblnLoaded = True

Finally, in the SelectionChanged event handler of the datagrid, I added the following:

If mblnLoaded = True Then

objStudentSelectListItem = CType _

( _

Me.dgStudent.SelectedItem, _

usp_StudentSelectList _

)

End If

Actually, I likethe behaviorthat causes the first item to be selected, it is only occurring once in my project.

---Sam

Sam Jazz  Wednesday, April 30, 2008 9:24 PM
Hi there,

I wonder if you could keep track of the current cell address and then filter out SelectionChanged events based on whether the contents of that cell are different. You might need some additional logic to SelectionChanged. I have a popular article about DataGridView and it has a section about SelectionChanged

http://dotnetperls.com/Content/DataGridView-Tips.aspx

Hope something helps a bit

Sam
smallen  Monday, May 26, 2008 8:35 PM
Depending on how you designed your form, you could just use
Code Snippet

if (!datagridviewobject.Focused) return;


as an entry point to your SelectionChanged code.
Rhyek  Tuesday, June 17, 2008 8:43 AM

I get the same problem loading data into the grid causes selection changed event to fire got round this in an earlier project by adding the handler after the load but not working on my latest project.

I like my microsoft products butthis datagridbehaviour is rubbish

ICCMPaul  Wednesday, June 25, 2008 2:00 PM

You can use google to search for other answers

Custom Search

More Threads

• BindingList with Images?
• When/Where should I instantiate the control when placing on the designer
• User Control resizes when placed on form (VS 2005)
• ImageList in VB.NET - Require VB6 Equivalent
• About the Icon resouces
• Custom dialog box
• Unable to maintain Viewstate when child controls events are fired
• User control resizing abnormal behavior
• how can i stop the IDE from being so "helpful?"
• CPU goes 100% when moving labels on a form