Solved

DataGridView - When a cell gets focus

Posted on 2010-09-06
9
387 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
[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
  • 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 30

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

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
 
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 30

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

728 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