none
How to get the row state of Datagridview? RRS feed

  • Question

  • Hello,

    I have a datagridview, the datasource is from the query result of LINQ:

    datagridview1.DataSource = query.ToList();

    User will be able to make the changes (insert/update/delete) on the datagridview. After the "Save" Button is pressed, I'd like to check the rowstate of this datagridview to do the insert/update/delete.

    I am able to check the rowstate for datatable, like:

    rows.RowState.ToString() == "Added"
    rows.RowState.ToString() == "Modified"

    BUT, cannot do it on the datagridview, please help on it.

    Thanks in advance!!!

    Tuesday, June 12, 2012 4:47 PM

Answers

  • State of the DataGridViewRow is not the same as the row state of the DataTable. If you want that you need to bind DataGridView to DataTable so that data item of each row is DataRow instance, then you can get that DataRow via DataBoundItem property and inspect the RowState of the bound DataRow instance.
    • Proposed as answer by Kris444 Wednesday, June 13, 2012 1:55 PM
    • Marked as answer by Mike FengModerator Friday, June 15, 2012 8:50 AM
    Tuesday, June 12, 2012 6:53 PM

All replies

  • You can use DataGridView.Rows property to get the DataGridViewRowCollection instance and then using Items property iterate over the rows and then use DataGridViewRow.State property to get the bitwise compination of the row state.
    Tuesday, June 12, 2012 5:27 PM
  • Hello MasaSam,

    Thank you very much for your reply!

    I tried to get the datagridviewrow.state, but it gives me "Displayed, Visible", "Displayed, Selected, Visible"... instead of "Added/Modified".

    Could you please give me a little sample code, thanks again!

    Tuesday, June 12, 2012 6:12 PM
  • State of the DataGridViewRow is not the same as the row state of the DataTable. If you want that you need to bind DataGridView to DataTable so that data item of each row is DataRow instance, then you can get that DataRow via DataBoundItem property and inspect the RowState of the bound DataRow instance.
    • Proposed as answer by Kris444 Wednesday, June 13, 2012 1:55 PM
    • Marked as answer by Mike FengModerator Friday, June 15, 2012 8:50 AM
    Tuesday, June 12, 2012 6:53 PM
  • Thank you!
    Tuesday, June 12, 2012 7:05 PM