1

I have ContextMenuStrip to show 2 Menu Item, and I use it on DataGridViewRow. I have a checkBoxColumn to select just 1 row to take an Id and used in another function. So this is my code to selrct the rows.

//get the selected item
List<DataGridViewRow> selectedRows = (from row in Detail_shanuDGV.Rows.Cast<DataGridViewRow>()
                                      where Convert.ToBoolean(row.Cells["checkBoxColumn1"].Value) == true
                                      select row).ToList();

if ((selectedRows.Count > 1) || (selectedRows.Count == 0))

    MessageBox.Show("Plz select au moin un ligne...");
else
{
    foreach (DataGridViewRow row in selectedRows)
    {
        //to do functions
    }
}

If I try used this code it always give me MessageBox.Show("Plz select au moin un ligne..."); but if I select another row it will show the last row selected.

My problems here that this code not working with ContextMenuStrip or MenuStrip, it works only with buttons.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Juste3alfaz
  • 295
  • 4
  • 21
  • as I said if I add that code into button Click, it will work – Juste3alfaz Nov 01 '16 at 14:24
  • So it seems the checked value has not been committed yet. Probably you checked a cell and right clicked on it. Is this the case? – Reza Aghaei Nov 01 '16 at 14:25
  • I check the the cell, than right click on the grid view, but if the checked not been committed, why the same code work with button?? – Juste3alfaz Nov 01 '16 at 14:27
  • 1
    Because when the cell lost focus the edit ends and value commits. See [this post](http://stackoverflow.com/a/40348082/3110834). – Reza Aghaei Nov 01 '16 at 14:28
  • thnx a lot, this new for me, and it works, just another question, is the position of the code "dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);" is important or not, because I add after the "List selectedRows.." at clicke item not worked, but the 2nd one works, but If I add before that code, it will work, any remark ? – Juste3alfaz Nov 01 '16 at 14:37
  • 1
    Side notes: you could just check on `selectedRows.Count != 1` and then in the else just do `selectedRows[0]` instead of the loop. Also your code formatting is kind of hard to follow with the space in the if statement and no braces so could benefit from a consistent style – TheLethalCoder Nov 01 '16 at 14:38
  • 1
    Yes the location is important. It should be before counting checked items. It can be a line before, it can be in `CellContentClick` event of `DataGridView`, it can be at `Opening` event of your context menu strip. I prefer to put it in `CellContentClick`. – Reza Aghaei Nov 01 '16 at 14:40
  • this code was originally to delete multiple records,, but I modified it to select 1 rows – Juste3alfaz Nov 01 '16 at 14:41
  • yes that what I did, thnx a lot Reza Aghaei – Juste3alfaz Nov 01 '16 at 14:42
  • You're welcome. I close the post as duplicate :) – Reza Aghaei Nov 01 '16 at 14:43
  • If you prefer to have an answer, please post the answer with some details using Post Your Answer button. Don't add solution to the answer. it makes the question confusing for future readers. If you are not going to post an answer, let me know to close the question :) – Reza Aghaei Nov 01 '16 at 14:46
  • ok, but I add ur link Post – Juste3alfaz Nov 01 '16 at 14:47
  • The problem is not including/not including the link to my answer :). As a general rule in the site, you should not post answer as part of the question. So if you prefer to have an answer in this question (which is useful IMO) just post an answer, you will have my vote. – Reza Aghaei Nov 01 '16 at 14:49
  • ok done I add a solution :) – Juste3alfaz Nov 01 '16 at 15:28
  • I see it, good job :) – Reza Aghaei Nov 01 '16 at 21:52

1 Answers1

1

Thanks to Reza Aghaei and his post here
I have my problems because the edited cell value is not committed to the DataSource until it's validated, which happens when the cell lose focus. If you want to commit the modifications immediately, you can handle the CurrentCellDirtyStateChanged event, and call the CommitEdit method in the handler : So, If for any reason you want to push changes sooner, you can call:

dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

This method commits every change just after the change is made.

So, for my code I add the code before selected item and it works, and the location of the commit is very important and preferred to put it in CellContentClick

    //get the selected item
Detail_shanuDGV.CommitEdit(DataGridViewDataErrorContexts.Commit);
List<DataGridViewRow> selectedRows = (from row in Detail_shanuDGV.Rows.Cast<DataGridViewRow>()
                                      where Convert.ToBoolean(row.Cells["checkBoxColumn1"].Value) == true
                                      select row).ToList();

if ((selectedRows.Count > 1) || (selectedRows.Count == 0))

    MessageBox.Show("Plz select au moin un ligne...");
else
{
    foreach (DataGridViewRow row in selectedRows)
    {
        //to do functions
    }
}
Community
  • 1
  • 1
Juste3alfaz
  • 295
  • 4
  • 21