Solved

Dynamically detect if row is selected in datagrid?

Posted on 2004-08-06
10
1,184 Views
Last Modified: 2008-02-01
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?
0
Comment
Question by:pillbug22
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 6

Author Comment

by:pillbug22
ID: 11736300
Oops...

Forgot to be polite and say thanks in advance for suggestions (and let me know if my explination was clear as mud).
-chris
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11736317
Right after you bind/fill the DataGrid.... Enable/Disable the buttons.
-Baan
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11736326
are you filling up the DataGrid through your code ?
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 6

Author Comment

by:pillbug22
ID: 11736372
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?
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11736398
No... unless you make it work that way.

Inside that Button_click you can disable/enable your buttons.
0
 
LVL 6

Author Comment

by:pillbug22
ID: 11736441
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.
0
 
LVL 4

Accepted Solution

by:
srcalc earned 125 total points
ID: 11737897
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
0
 
LVL 6

Author Comment

by:pillbug22
ID: 11738285
That pretty much did it - had to make 1 or two adjustments, but that definately got me going in the right direction!
0
 

Expert Comment

by:Duc_de_Belfort
ID: 11891237
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
0
 
LVL 6

Author Comment

by:pillbug22
ID: 11891337
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question