Solved

DataGridView - When a cell gets focus

Posted on 2010-09-06
9
378 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

19 Experts available now in Live!

Get 1:1 Help Now