4 Replies - 440 Views - Last Post: 13 July 2012 - 05:32 AM Rate Topic: -----

#1 maiOHmai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 09-July 12

how to populate textbox when dgvcell is clicked?

Posted 09 July 2012 - 08:25 AM

i have a form that is named MainFrm. this form contains a textbox called txtshow wherein when i hit the enter key, data from the table BorrowersList in the database will be shown in the datagridview(dgvSearch). BorrowersList table has 10 columns and dgvSearch will only show 2 columns out of this 10. What i want to happen is that, when I clicked a cell in the dgvSearch textbox1 will be populated with Section(1 of the 10 columns).
Here is my code on the MainFrm:
Imports System.Data.OleDb
Public Class MainFrm
    Dim con As New OleDbConnection
    Dim da As New OleDbDataAdapter

    Private Sub AddBoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddBoToolStripMenuItem.Click
        AddBorrowerFrm.Show()
        Me.Enabled = False
    End Sub

    Private Sub LogOutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LogOutToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub MainFrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con.ConnectionString = "provider = Microsoft.ACE.OLEDB.12.0; data source = ..\sample.accdb"

        If lblLMCon.Text = "admin" Then
            AddBoToolStripMenuItem.Enabled = True
            DeleteBorrowerToolStripMenuItem.Enabled = True
        Else
            AddBoToolStripMenuItem.Enabled = False
            DeleteBorrowerToolStripMenuItem.Enabled = False
        End If
    End Sub

    Private Sub DeleteBorrowerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteBorrowerToolStripMenuItem.Click
        cmbTran.Enabled = False

    End Sub

    Private Sub txtShow_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtShow.KeyDown
        da = New OleDbDataAdapter("Select ID, (LName + ', ' + FName) As FullName from BorrowersList where ID = '" & txtShow.Text & "'", con)
        con.Open()
        Dim ds As DataSet = New DataSet()
        'Dim ds As As sampleDataSet.BorrowersListRow
        da.Fill(ds, "BorrowersList")
        dgvSearch.DataSource = ds.Tables("BorrowersList").DefaultView
        con.Close()


    End Sub

    Private Sub dgvSearch_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvSearch.CellDoubleClick
        Dim i As Integer
        i = dgvSearch.CurrentRow.Index
        txtFullName.Text = dgvSearch.Item(1, i).Value
        Dim a As Integer
        a = dgvSearch.Item("Gender").ToString
        dgvSearch.Rows(a).Cells(0).Value = TextBox1.Text


        'Dim drvBorrower As DataRowView
        ' Dim rowBorrower As sampleDataSet.BorrowersListRow
        ' drvBorrower = dgvSearch.Rows(e.RowIndex).DataBoundItem
        ' rowBorrower = drvBorrower.Row
        ' TextBox1.Text = rowBorrower._Borrower_sID

        'da = New OleDbDataAdapter("Select * BorrowersList where ID = '" & txtShow.Text & "'", con)
        'con.Open()
        'Dim ds As DataSet = New DataSet()
        'da.Fill(ds, "BorrowersList")

        'TextBox1.Text = ds.Tables("BorrowersList").Rows("ID")
        'con.Close()

       
    End Sub
End Class



those with ['] are the codes i've tried to get an output but none has work.

thank you.

Attached image(s)

  • Attached Image

This post has been edited by modi123_1: 09 July 2012 - 08:31 AM
Reason for edit:: highlight the lines THEN click the code tags button in the 'format text' box of the post.


Is This A Good Question/Topic? 0
  • +

Replies To: how to populate textbox when dgvcell is clicked?

#2 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 93
  • View blog
  • Posts: 871
  • Joined: 15-October 10

Re: how to populate textbox when dgvcell is clicked?

Posted 09 July 2012 - 08:57 AM

Hey have a look at this:

For Each cell In DataGridView1.SelectedCells
                'Get the colum index from the cell selected column's index
                txtwhatever = DataGridView1.CurrentCell.Value.ToString
Next 


This may need tweek abit but should point you in the right direction

just looking at the above code that is not the right way to go about this use the CellDoubleClick event


Private Sub FilterPOSites(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        Try
            If e.ColumnIndex <> -1 Then
                txtwhatever = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString.ToUpper
                col_Count = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)
                Else
                    Exit Sub
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

This post has been edited by m_wylie85: 09 July 2012 - 09:03 AM

Was This Post Helpful? 0
  • +
  • -

#3 maiOHmai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 09-July 12

Re: how to populate textbox when dgvcell is clicked?

Posted 10 July 2012 - 03:22 AM

what the use of "col_Count = Datagridview1.Rows(e.RowIndex).Cells(e.ColumnIndex)"?

what's the use of "col_Count = Datagridview1.Rows(e.RowIndex).Cells(e.ColumnIndex)"?
Was This Post Helpful? 0
  • +
  • -

#4 maiOHmai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 09-July 12

Re: how to populate textbox when dgvcell is clicked?

Posted 10 July 2012 - 03:38 AM

the code only shows the ID (which is in the first column). what i want to happened is to display data in the textbox that dgv does not contains. For example, dgvsearch contains ID, and Fullname, in my textbox, i want to display her gender

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#5 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 232
  • View blog
  • Posts: 1,309
  • Joined: 07-April 08

Re: how to populate textbox when dgvcell is clicked?

Posted 13 July 2012 - 05:32 AM

if you want to display data that is not in your datagridview then you're going to need to either

A) Pull back all record data you are going to want to use and store it in a datatable or some other List. Then bind that list to your datagrid. This will allow you to then be able to gather the ID of the row clicked, and pull that record out of what ever storage you are using, and then pick out the gender from that list. (sounds more complicated then it actually is. I'll write up an example and post it)

B ) Make a second call to the database to get the gender of the person selected. (Not a good way of doing it)

This post has been edited by rgfirefly24: 13 July 2012 - 05:35 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1