Solved

DataGridView help

Posted on 2008-10-10
10
605 Views
Last Modified: 2013-12-17
Experts,

I am new to development. I am using VS2008 and C#. I am I am trying to fill different controls and other things on my form based on the value of certain columns in the current row of a DataGridView control. For example one line of code is:
comboTech1.Text = wd_help_deskDataGridView.CurrentRow.Cells["help_tech"].Value.ToString();

My question is, what DGV event should I use to fire when a new row gets focus. I tried using RowEnter event but the CurrentRow value is not set when this event triggers. RowStateChanged also seems to have the same problem. The only event I've been somewhat successful with is RowPrePaint but this fires multiple times when the DGV populates each line and it does not seem very efficient. Is there an event I am not seeing or am I going about this wrong?

Thanks,
Chad
0
Comment
Question by:kalittaair
  • 5
  • 4
10 Comments
 
LVL 4

Expert Comment

by:jlj1527
ID: 22692332
Try SelectionChanged Event
0
 

Author Comment

by:kalittaair
ID: 22692338
Thx for the quick response. But won't that event also fire if the user changes to a different cell in the same row? I only want it to take place if the row changes.

Thanks,
Chad
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22692388
Hi kalittaair,

Use the RowEnter event -> http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowenter.aspx

Regards,

Wayne
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:kalittaair
ID: 22692528
Wayne,

You'll notice my example uses the CurrentRow value. The CurrentRow value is not set on a RowEnter event. When RowEnter fires, the value of the CurrentRow is actually the row that just lost the focus, not the new one. If i use this event, how do I determine which row just got the focus?

Thanks,
Chad
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22692937
wd_help_deskDataGridView.Rows(e.RowIndex)
0
 

Author Comment

by:kalittaair
ID: 22693678
I see. I use the RowEnter event but rather then use the CurrentRow value to find the new row I use e.Rowindex? I'll try that and report back. How would I pick out a specific column's value using that? In other words, can you re-write this line of code using the e.Rowindex:
comboTech1.Text = wd_help_deskDataGridView.CurrentRow.Cells["help_tech"].Value.ToString();

Thanks,
Chad
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22693738
comboTech1.Text = wd_help_deskDataGridView.Rows(e.RowIndex).Cells["help_tech"].Value.ToString();
0
 

Author Comment

by:kalittaair
ID: 22694152
webtubbs:

Thanks for the code, but I get:
"Non-invocable member 'System.Windows.Forms.DataGridView.Rows' cannot be used like a method."

As I was typing it, when I got to (e.RowIndex). Intellisense did not allow me to pick "Cells" so I figured it was going to bomb. Any ideas?
0
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 250 total points
ID: 22695936
Sorry, that should have been like this....

    comboTech1.Text = wd_help_deskDataGridView.Rows[e.RowIndex].Cells["help_tech"].Value.ToString();

Wayne
0
 

Author Closing Comment

by:kalittaair
ID: 31505244
Thanks Wayne!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

778 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