11 Replies - 22307 Views - Last Post: 08 February 2012 - 07:00 AM Rate Topic: -----

#1 dred05   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-February 12

selected datagrid value to textbox

Posted 07 February 2012 - 05:22 AM

I just started learning programming today so I'm a complete noob. Anyway I'm trying to dispaly the cell I selected from the datagrid inside a textbox but it's not working. Can you tell me what's wrong. I included the other codes just in case.

 Imports System.Data.OleDb
Public Class Form1
    Dim con As New OleDbConnection


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con.ConnectionString = " Provider = microsoft.jet.oledb.4.0; data source = c:\userpass.mdb"

        con.Open()

        datagridShow()


    End Sub


    Private Sub datagridShow()

        Dim ds As New DataSet
        Dim dt As New DataTable
        ds.Tables.Add(dt)
        Dim da As New OleDbDataAdapter

        da = New OleDbDataAdapter("select * from userpass", con)

        da.Fill(dt)

        DataGridView1.DataSource = dt.DefaultView

        con.Close()



    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick


        Dim i As Integer

        i = DataGridView1.CurrentRow.Index

        Me.TextBox2.Text = DataGridView1.Item(1, i).Value

End Sub




Also the "DataGridView1.Item(1, i).Value" Keeps getting highlighted it says "Implicit conversion from Object to String."

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: selected datagrid value to textbox

#2 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1824
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: selected datagrid value to textbox

Posted 07 February 2012 - 05:42 AM

Ok now the problem is here probably:
i = DataGridView1.CurrentRow.Index
Me.TextBox2.Text = DataGridView1.Item(1, i).Value


I may say two things:
-First you set i as current row index, so it presents row, Ok. But you said you need a cell you selected and you always make the column index to 1 Item(1, i)

- About "Implicit conversion from Object to String" is because the Value property returns Object typed value and you need to assign it to string. So you have to change it to string by applying ToString() method at the end.
Hope this helped

Also welcome to Dream In Code, good to see new members followed the rules.

This post has been edited by smohd: 07 February 2012 - 05:44 AM

Was This Post Helpful? 0
  • +
  • -

#3 Ionut   User is offline

  • D.I.C Lover
  • member icon

Reputation: 386
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: selected datagrid value to textbox

Posted 07 February 2012 - 05:42 AM

1. DataGridViewCellEventArgs class has two properties called RowIndex and ColumnIndex. In DataGridView1_CellContentClick you have e parameter which is an object of type DataGridViewCellEventArgs. This means you can do something like
Me.TextBox2.Text = DataGridView1.Item(1, e.ColumnIndex).Value


The reason I tell you this is that i variable is used as column identifier, even if you declared it as CurrentRow index. If CurrentRow is higher than the index of the last column, you will get an error(eg. your grid has 5 columns and you select row 10 ==> error because you don't have 10 columns)
2. Value property of the DataGridViewCell class is of type System.Object. Text property of TextBox control is of type String. An implicit conversion cannot occur. This means you have to call ToString method to make the conversion.
Me.TextBox2.Text = DataGridView1.Item(1, e.ColumnIndex).Value.ToString


Was This Post Helpful? 0
  • +
  • -

#4 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1824
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: selected datagrid value to textbox

Posted 07 February 2012 - 05:55 AM

@ Ionut: The item property definition is Item(ColumnIndex As Integer, RowIndex As Integer). So it should be vice vesa. So he will need the i and e.ColumnIndex stays at position where he has 1
Was This Post Helpful? 0
  • +
  • -

#5 dred05   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-February 12

Re: selected datagrid value to textbox

Posted 07 February 2012 - 06:23 AM

Thanks for answering!


But how about the datagrid to textbox. Nothing appears on my textbox whenever I click something inside the datagrid. Should I change the CurrentRow to CurrentCell?

:/
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: selected datagrid value to textbox

Posted 07 February 2012 - 08:47 AM

OK, CurrentCell is the currently active cell. So if you were to use
TextBox2.Text = DataGridView1.CurrentCell.Value.ToString
Then the TextBox would be filled with whatever is in the cell you selected.

You could also use something like
TextBox2.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString
where in this case, when you select a cell, the 2nd columns value of the row you're on would display in the textbox.

Again, as Ionut stated, even though he showed the columns and rows reversed in his example, the statement still stands that the eventargs are still a great way to retrieve this information when using the item property as a variable. So by his example, simply changing your code to:
TextBox2.Text = DataGridView1.Item(1, e.RowIndex).Value.ToString
should produce favorable results. This mean that you no longer need anything else but that line in the CellContentClick event.
Was This Post Helpful? 0
  • +
  • -

#7 dred05   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-February 12

Re: selected datagrid value to textbox

Posted 08 February 2012 - 03:48 AM

Thanks again for answering CharlieMay.


I tried all the 3 codes you gave but none of them works :/

Maybe there's something wrong with my connection code? I'm so confused.
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: selected datagrid value to textbox

Posted 08 February 2012 - 05:12 AM

Well, does the datagridview fill with the information from the database? If not, that would be a pretty good indication if something was wrong with your code to fill a datasource and set it to the datagridview.

Are you getting any exceptions in your immediate window when the program runs?
Was This Post Helpful? 0
  • +
  • -

#9 dred05   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-February 12

Re: selected datagrid value to textbox

Posted 08 February 2012 - 06:00 AM

Well no the data shows fine and I'm not encountering any errors or exceptions. I really have no idea what's wrong...
Was This Post Helpful? 0
  • +
  • -

#10 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: selected datagrid value to textbox

Posted 08 February 2012 - 06:06 AM

Without knowing everything about your program and what all controls you have I guess I can only ask if you're sure you're accessing the right datagridview and textbox? I only ask this because I don't know if you have more than one of these objects on your form and are just getting confused with which one you're using. Other than that, re-post what you have now. Because I copied the code above, made the suggested changes, changed to my database and it works as I expect it to work.
Was This Post Helpful? 0
  • +
  • -

#11 dred05   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-February 12

Re: selected datagrid value to textbox

Posted 08 February 2012 - 06:30 AM

These are the only codes that I currently have

Imports System.Data.OleDb
Public Class Form1
    Dim con As New OleDbConnection
    Dim ds As New DataSet
    Dim dt As New DataTable
    Dim da As New OleDbDataAdapter
    Dim i As Integer


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con.ConnectionString = " Provider = microsoft.jet.oledb.4.0; data source = C:\Users\Judz\Desktop\userpass.mdb"

        con.Open()

        datagridShow()

    End Sub

    Private Sub datagridShow()

       
        ds.Tables.Add(dt)

        da = New OleDbDataAdapter("select * from userpass", con)

        da.Fill(dt)

        DataGridView1.DataSource = dt.DefaultView

        con.Close()



    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick


    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick



        TextBox1.Text = DataGridView1.Item(0, e.RowIndex).Value.ToString
        TextBox2.Text = DataGridView1.Item(1, e.RowIndex).Value.ToString
        TextBox3.Text = DataGridView1.Item(2, e.RowIndex).Value.ToString


    End Sub





And here's my form

Posted Image


I already checked the names of my textboxes and datagrids a couple of times no problems there.

Crap!!! LOL I already figured out the problem as soon as I posted my message!

I have two of these

"Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick"


*facepalm*

And as soon as I deleted the other one it worked.

Thank you so much for the help. Should've double checked it.

*facepalm*

:P

and sorry to bother you guys... :sweatdrop:
Was This Post Helpful? 0
  • +
  • -

#12 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: selected datagrid value to textbox

Posted 08 February 2012 - 07:00 AM

OK, you actually brought up a problem, at first I didn't understand how that empty event would affect the results and then I saw that this only works if you click the text contained in the cell. You should really move your code into the CellEnter event where it will work when a cell gets focus.

DataGridView Events

This post has been edited by CharlieMay: 08 February 2012 - 07:05 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1