Data Grid View modifying rows on click
-
Monday, August 15, 2011 8:12 PM
I cannot for the life of me figure out why my data grid view is altering the ROWstate of rows that I click on.
When a row is selected, it changees the row state to modify.
There are no events in the data grid view. I checked all the properties to see if there was something but it all seems fine.
Can anyone think of anything that would be causing this? I've spent hours trying to figure it out -_-
All Replies
-
Wednesday, August 17, 2011 8:12 AMModerator
Hi CommanderKeen,
Is that the RowStated changed when you click or edit the row? If it's click, then I cannot reproduce the problem on the condition as you say "are no events in the data grid view", I just subscribed a RowStateChanged event as bellow but nothing changed when I click on the DataGridView. If I edit the cell for one row, the RowStatedChanged event will fire, but not when I click on the row.
private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
this.Text = this.Text + "aa";
}Make sure there's no event of DataGridView like CellClick or CellContentClick interfiers the RowState . Also, is there any other control's event that potencially modify the row's state? If you still have no idea, please post an sample that can reproduce the problem. It is not necessary that you send out the complete source of your project. We just need a simplest sample to reproduce the problem. You can remove any confidential information or business logic from it.
Just for you information : DataGridView.RowStateChanged Event Occurs when a row changes state, such as losing or gaining input focus.
Helen Zhou [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

-
Wednesday, August 17, 2011 7:01 PM
Thank you for your response. With your information I created another test.
I narrowed it down to the property SelectionMode of the dataGridView. if I set this to FullRowSelect the RowStateChanged will fire.
Do you have an explanation as to why this is the case?
-
Wednesday, August 17, 2011 7:19 PM
The strange this is. After the RowStateChange fires I print out the rowstate of all the rows in the dataset and they all read Unchanged.
So I am thinking the rowstate changed means something different than the actual rowstate changing.
I am misunderstanding the point of RowStateChange, essentially.
-
Tuesday, August 23, 2011 9:40 AMModerator
I guess you may misunderstand what RowState means, it mean any cell's value of the row is changed or not, so if you edit a cell, rowState will change, if you just click on the cell, rowState will not change, if you change another cell's value in the same row, this row's rowState will not change.
If you set SelectionMode to FullRowSelect, then both click and edit a cell will change the RowState.
You said "After the RowStateChange fires I print out the rowstate of all the rows in the dataset and they all read Unchanged.", I don't know when did you print, but I think it will be truely handled in the RowStateChange event, not out of the event.
Helen Zhou [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

-
Wednesday, August 24, 2011 5:16 PM
Thanks for the response.
Some things I'm not clear on based on my test. You say:
"if you change another cell's value in the same row, this row's rowState will not change."
I created a button and when I press it it changes the value of the current row:
private void button2_Click_1(object sender, EventArgs e) { dataGridView1.CurrentRow.Cells[1].Value = "Altered!"; }
I created another button that prints out the rowState of the underlying dataTable bound to he dataGridView1:
private void button1_Click_1(object sender, EventArgs e) { foreach (DataRow dtrRow in dtsBot.ttlRole.Rows) { Trace.WriteLine("State of row: " + dtrRow.RowState); } }
When I finish the edit (move to a new row) and click button1 the output is:
State of row: Unchanged
State of row: Unchanged
State of row: Modified
State of row: Unchanged
State of row: Unchanged
State of row: Unchanged
Your next statement:
"If you set SelectionMode to FullRowSelect, then both click and edit a cell will change the RowState."
I don't understand how this is true either.
When I click a row the RowStateChanged does INDEED fire. But when I click the Button1 event it prins out:
State of row: Unchanged
For ALL rows. So I do not understand why the RowStateChanged even is firing when the states of the row are indeed not changing.
This is what I mean when I say I print out the row states AFTER the RowStateChanged has fired.
So I guess there is still something I don't understand about the whole process.
Thanks for your time!
-
Friday, July 27, 2012 11:44 AM
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowstatechanged%28v=vs.90%29.aspx
this link clearly shows that the RowStateChanged event occurs when a row changes state, such as losing or gaining input focus.


