Dynamically detect if row is selected in datagrid?

Hi everyone-

I have a win form with a dataGrid which acts a queue before choosing a command for the queue of items: add [rows in datagrid to database], delete all[rows from datagrid], dlete selected [row from datagrid].

For UI improvements, I'm trying to make only the available options enabled: i.e. if no row exists in the datagrid, the "Add All" and "Delete All" buttons are disabled.

I have code that pulls the row numbers of the selected rows (I can delete the selected rows w/o a problem), but where do I call this code from so that it enabled/disables the "Delete Selected" button based on if the user just highlighted a row(s) or not? Should I put it in the _onPaint event (doesn't seem efficient). Maybe in the _KeyPress and _Click events of the form?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pillbug22Author Commented:

Forgot to be polite and say thanks in advance for suggestions (and let me know if my explination was clear as mud).
Right after you bind/fill the DataGrid.... Enable/Disable the buttons.
are you filling up the DataGrid through your code ?
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

pillbug22Author Commented:
Yes, the user types the info in text boxes, then hits a button which adds the info to a dataTable (starts empty).

The dataGrid isn't re-bound to its source each time you select/deselect a row, is it?
No... unless you make it work that way.

Inside that Button_click you can disable/enable your buttons.
pillbug22Author Commented:
It needs to run based on whether they have selected a row or not, not based on the button.

When they add their info to the list, there just becomes a long list (grid) of info. Then they can click on a row and say "I don't want that one and more", kind of like removing an item from your online shopping cart, but we only want the delete button enabled if they have selected a row first.
try using the DataGrid.CurrentCellChanged event:

    Private Sub DataGrid_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid.CurrentCellChanged
'Check to see if any rows are selected...
    End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pillbug22Author Commented:
That pretty much did it - had to make 1 or two adjustments, but that definately got me going in the right direction!
you accepted to early....

the information if a row is selected can easily be answered:

    If myDataGrid.IsSelected(0) Then
        MessageBox.Show("Row selected", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        MessageBox.Show("Row not selected", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End If


regards, duc
pillbug22Author Commented:

thanks for the input, but doing the actual check wasn't the issue. I was needing a way to run the check based on events, not a button click. I needed the button to change the .enabled property whenever the dataGrid was updated/changed.

Thus the _CurrentCellChanged event worked out.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.