Results 1 to 10 of 10

Thread: [RESOLVED] [2005] DataGridView Selected Cell

  1. #1

    Thread Starter
    Hyperactive Member Jonny1409's Avatar
    Join Date
    Mar 2005
    Posts
    308

    Resolved [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:
    1. 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.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] DataGridView Selected Cell

    Try this
    VB Code:
    1. Textbox1.text = Datagridview1.CurrentCell.Value

  3. #3

    Thread Starter
    Hyperactive Member Jonny1409's Avatar
    Join Date
    Mar 2005
    Posts
    308

    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.

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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:
    1. 'This will populate the textbox with the value in the 1st cell of the selected row
    2.         TextBox1.Text = DataGridView1.CurrentRow.Cells(0).Value.ToString

  5. #5
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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:
    1. Private Sub DataGridView1_CellClick(...) Handles DataGridView1.CellClick
    2.         MessageBox.Show(DirectCast(sender, DataGridView).CurrentCell.Value.ToString)
    3. End Sub

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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:
    1. Private Sub DataGridView1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellChanged
    2.         If Not DataGridView1.CurrentRow Is Nothing Then
    3.             'This will populate the textbox with the value in the 1st cell of the selected row
    4.             TextBox1.Text = DataGridView1.CurrentRow.Cells.Item(0).Value.ToString
    5.         End If
    6.     End Sub

  7. #7

    Thread Starter
    Hyperactive Member Jonny1409's Avatar
    Join Date
    Mar 2005
    Posts
    308

    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.

  8. #8
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] DataGridView Selected Cell

    I tryed and both events mentioned above work for me. Can you show your code?

  9. #9

    Thread Starter
    Hyperactive Member Jonny1409's Avatar
    Join Date
    Mar 2005
    Posts
    308

    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.

  10. #10
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width