Solved

DataGridView - When a cell gets focus

Posted on 2010-09-06
9
381 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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