Link to home
Start Free TrialLog in
Avatar of pillbug22
pillbug22

asked on

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?
Avatar of pillbug22
pillbug22

ASKER

Oops...

Forgot to be polite and say thanks in advance for suggestions (and let me know if my explination was clear as mud).
-chris
Right after you bind/fill the DataGrid.... Enable/Disable the buttons.
-Baan
are you filling up the DataGrid through your code ?
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of srcalc
srcalc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)
    Else
        MessageBox.Show("Row not selected", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End If


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWindowsFormsDataGridClassIsSelectedTopic.asp

regards, duc
Duc,

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.