0

i have a DataGridView in my program that has a DataGridViewButtonColumn. When a user clicks on that button, it shows more detail on another panel.

I am having trouble trying to do the following:

Click the "send" button on an ordering screen, that will send the order to the database, and the DataGridView will automatically update, but the user still will need to click on the corresponding column button on the row to view more detail.

What i want to do is that when the user clicks send, it will automatically find the row that corresponds to the orderID, and automatically click that button.

this is what i have:

  foreach(DataGridViewRow row in OutOrderListGridView.Rows){
                string compareID = row.Cells[0].Value.ToString();
                if (compareID == OrderID)
                {
                    row.Cells[10].Selected = true;  <-- here i want to performClick() on the columnbutton(cell[10]) on the specific row.
                }
            }

Thank you.

RussDunn
  • 175
  • 1
  • 12
cutecutebj
  • 179
  • 1
  • 15
  • Does each row have its own "Send" button? WinForms or WPF? – Dasanko Apr 20 '15 at 06:38
  • WindForms, each row has its own button, let says i wanna click row 5's button which is located at column 10, i would like to performclick on that button. i know there is performclick() for normal buttons, but I wonld like to do the same thing to a columnbutton – cutecutebj Apr 20 '15 at 08:46

1 Answers1

1

I am not 100% sure i quite understand what you are after, but if i am correct you want to do the following provided the CellClick event is handling whatever you are trying to ultimately achieve.

Instead of 'performing' the click ( PerformClick() ) as such you can raise the event manually with known values... This should work for you but is slightly untested in your particular application given unknown variables. Code below also had to be slightly reworked to suit.

for (int i = 0; i < OutOrderListGridView.RowCount; i++)
  {
      if (OutOrderListGridView[0, i].Value.ToString() == OrderID)
      {
          OutOrderListGridView_CellClick(OutOrderListGridView, new DataGridViewCellEventArgs(10, i));
          break;
      }
  }

The foreach loop had to be changed to a for loop in order to make use of the current index (position of the loop) that a foreach loop does not provide.

The string comparison did not need to be two stepped so was condensed accordingly.

I have also added a break to the loop as i would think that once you found your match, it would be pointless to iterate through the remaining rows.

RussDunn
  • 175
  • 1
  • 12
  • thanks for the reply, i will test it out once i get back to my computer – cutecutebj Apr 20 '15 at 09:33
  • 1
    This looks like what I was thinking the solution should be. Just one edit: For the `DataGridViewButtonColumn` you should use `CellContentClick` vs `CellClick` as nicely explained [here](http://stackoverflow.com/a/13687844/3773066). – OhBeWise Apr 20 '15 at 20:23
  • @OhBeWise Good point. I guess at the end of the day it would be implemented however the cell button click was being handled in the first place but +1 for using CellContentClick over CellClick – RussDunn Apr 20 '15 at 21:59