[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.
Re: [2005] DataGridView Selected Cell
Try this
VB Code:
Textbox1.text = Datagridview1.CurrentCell.Value
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 ?
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
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
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
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.
Re: [2005] DataGridView Selected Cell
I tryed and both events mentioned above work for me. Can you show your code?
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.
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).