|
-
Dec 21st, 2006, 11:09 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [2005] DataGridView Selected Cell
Hello,
What I'm after is probably really simple, but I can't figure it out.
I need the selected cell in a DataGridView.
So for example, in my DataGridView I have the following :
Col A - Col B - Col C
AAA - BBB - CCC
If I click onto any of the cells (AAA, BBB or CCC), I would like the value 'BBB' to be displayed in my textbox.
To do this in a Datagrid, I would use :
VB Code:
Textbox1.text = Datagridview1.Item(Datagridview1.CurrentRowIndex, 1).ToString)
However, I want to use a DataGridView due to the entended properties is gives me.
Thanks,
J.
Last edited by Jonny1409; Dec 21st, 2006 at 11:14 AM.
-
Dec 21st, 2006, 11:20 AM
#2
Re: [2005] DataGridView Selected Cell
Try this
VB Code:
Textbox1.text = Datagridview1.CurrentCell.Value
-
Dec 21st, 2006, 11:29 AM
#3
Thread Starter
Hyperactive Member
Re: [2005] DataGridView Selected Cell
Hi stanav,
Thanks for responding - but that doesn't seem to work correctly.
Basically it has the following problems :
1) I must click onto the actual text, not the cell - so if I have "BBB" in a cell, it only populates if I click onto the "BBB", not if I click into the space to the right of the letters.
2) If I click onto any cell this gives me the value, so if I click onto "CCC", the box is populated with "CCC", whereas I want it populated with "BBB" regardless of what cell I click on.
Does this make sense ?
Last edited by Jonny1409; Dec 21st, 2006 at 11:33 AM.
-
Dec 21st, 2006, 01:08 PM
#4
Re: [2005] DataGridView Selected Cell
Okie, so I misunderstood your original question... I thought you wanted to populate the textbox with whatever value the current cell in the datatgidview has, but in fact you want only the value form a certain cell on a selected row.
Try this then
VB Code:
'This will populate the textbox with the value in the 1st cell of the selected row
TextBox1.Text = DataGridView1.CurrentRow.Cells(0).Value.ToString
-
Dec 21st, 2006, 01:23 PM
#5
Re: [2005] DataGridView Selected Cell
Handle the .CellClick event of the DatagridView and change the textbox text to what you want. The below code displays the current cell contents of the clicked cell in a messagebox...
VB Code:
Private Sub DataGridView1_CellClick(...) Handles DataGridView1.CellClick
MessageBox.Show(DirectCast(sender, DataGridView).CurrentCell.Value.ToString)
End Sub
-
Dec 21st, 2006, 01:42 PM
#6
Re: [2005] DataGridView Selected Cell
Actually, it's better to use the CurrentCellChanged event in this case since it will fire even when the user navigate the datagridview using keyboard.
VB Code:
Private Sub DataGridView1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellChanged
If Not DataGridView1.CurrentRow Is Nothing Then
'This will populate the textbox with the value in the 1st cell of the selected row
TextBox1.Text = DataGridView1.CurrentRow.Cells.Item(0).Value.ToString
End If
End Sub
-
Dec 22nd, 2006, 03:48 AM
#7
Thread Starter
Hyperactive Member
Re: [2005] DataGridView Selected Cell
Hi stanav,
Thanks again for this - it now works exactly as I want it to apart from one thing - in order to populate the textbox I need to click onto the actual letters in the box. If I click onto the white area to the right of the letters it doesn't work.
Is there any way around this ?
Thanks,
J.
-
Dec 22nd, 2006, 04:07 AM
#8
Re: [2005] DataGridView Selected Cell
I tryed and both events mentioned above work for me. Can you show your code?
-
Dec 22nd, 2006, 04:22 AM
#9
Thread Starter
Hyperactive Member
Re: [2005] DataGridView Selected Cell
Sure - my code is :
TextBox1.Text = DataGridView1.CurrentRow.Cells.Item(0).Value.ToString
As I say this works fine and populates the textbox with the middle column's data. However I need to click on the actual text in the cell, not just the cell itself.
Forget about it, I've actually sorted it.
I had the code on the 'CellContentClick' event of the DataGridView. I have changed this to the 'CellClick' event and it's fine.
Cheers though,
J.
-
Dec 22nd, 2006, 04:33 AM
#10
Re: [RESOLVED] [2005] DataGridView Selected Cell
Yes, I was thinking that you have your code in "CellContentClick" event so that it fires when only the content of the cell is clicked.
Now, I don't know what your needs are so I would suggest using the "CellClick" event if you only want to change the textbox value when the cell is clicked.
The "CurrentCellChanged" event would be more appropriate if you want to have the textbox value change if the cell value is changed plus this event will fire if the cell is clicked or UP, Down, Left, Right, Tab keys are pressed (if you use this event you need the condition "If" statement as stanav shows).
Last edited by VBDT; Dec 22nd, 2006 at 04:41 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|