Solved

DataGridView - When a cell gets focus

Posted on 2010-09-06
9
379 Views
Last Modified: 2012-05-10
Hi

What event should I be using in a datagridview when I want to put the current value of the datagridview value in a textbox.

When a user clicks in a cell or moves the arrow keys up/down to move to a cell, I would like to catch the new cell value and display that in a textbox.

Can someone give me an idea on how to go about this.

Thanks.
0
Comment
Question by:elmbrook
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 10

Expert Comment

by:james-ct16
ID: 33615091
Howdy

You probably want to be looking at handling the cellendedit event, msdn article and examples are here http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellendedit.aspx

Hope that helps

James
0
 
LVL 6

Expert Comment

by:tikusbalap
ID: 33615234
You can handle event on grid.CellContentClick or grid.CellValueChanged.
0
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 33615242
I think CellEnter is the best for your case. See the snippet:
        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)

        {

            this.textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue.ToString();

        }

Open in new window

0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 33615253
cellendedit event is not suitable - you are not editing anything!
CellContentClick  - also bad idea - what if I navigate using a keyboard?!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

by:james-ct16
ID: 33615300
Howdy

My apologies I read the question to quickly and miss the point, I would be using the CellStateChanged event (http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellstatechanged.aspx) and looking for the state selected (http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewelementstates.aspx) then once the cell is selected set the value to the textbox.

This should account for mouse and keyboard selection, I also agfree with anarki jimbel that cell content click would be a bad idea.

James

James
0
 
LVL 6

Expert Comment

by:aymoon80
ID: 33615404
yourTextBox.Text = yourDataGrid.CureentCell.FormattedValue.ToString();
0
 
LVL 6

Expert Comment

by:aymoon80
ID: 33615414
the event is CellClick event
0
 
LVL 6

Expert Comment

by:aymoon80
ID: 33615441
correction:

yourTextBox.Text = yourDataGrid.CureentCell.EditedFormattedValue.ToString();

Or (the same)

yourTextBox.Text = yourDataGrid[e.ColumnIndex, e.RowIndex]..EditedFormattedValue.ToString();
____________
to get the value not the displayed value in case of combo box use Value instead of EditedFormattedValue

Regards
Ayman
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 33621781
aymoon80, I believe "CureentCell" means "CurrentCell"? :)

I just wonder if anyone here tested his code?! I tested and it worked perfectly :) BTW, it took not more than 3- 4 minutes. "CureentCell" code wasn't tested definitely...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now