[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

DataGridView - When a cell gets focus

Posted on 2010-09-06
9
Medium Priority
?
390 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 2000 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

Industry Leaders: 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!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

656 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