11 Replies - 22169 Views - Last Post: 27 June 2014 - 08:05 AM Rate Topic: -----

#1 manutd4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-May 08

Display datagridview selected row in textboxes

Posted 12 August 2009 - 09:35 PM

Hi every1, i never work with datagridview and i need to make the selected row displayed in textboxes.
For example i had three textboxes (txt1, txt2, txt3) and the datagridview had three columns (name, surname, address).
The datagridview in connected with an access database using oledbconnection.
So i wanted to select a row and then display the selected row in there respective textboxes.
The name goes in txt1, the surname goes in txt2, the address goes in txt3
please help
thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Display datagridview selected row in textboxes

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: Display datagridview selected row in textboxes

Posted 12 August 2009 - 10:15 PM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: :code:

Thanks.
Was This Post Helpful? 0
  • +
  • -

#3 manutd4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-May 08

Re: Display datagridview selected row in textboxes

Posted 12 August 2009 - 10:39 PM

Here's the code:
Private Sub DataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView.CellClick
		dSet.Tables(0).PrimaryKey = New DataColumn() {dSet.Tables(0).Columns("WatchID")}
		Dim row As DataRow
		row = DataGridView.Rows(DataGridView.SelectedCells(0).RowIndex).Cells(0).Value
		txt1.Text = row("name")
		txt2.Text = row("surname")
		txt3.Text = row("address")
	End Sub

i dont know whether the code is good or wrong cause i never work with datagridview
so please help
Was This Post Helpful? 0
  • +
  • -

#4 manutd4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-May 08

Re: Display datagridview selected row in textboxes

Posted 13 August 2009 - 01:53 AM

ok i got it,
here's the code:
	Private Sub DataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView.CellClick
		Dim i As Integer
		i = DataGridView.CurrentRow.Index
		Me.txtSupplierId.Text = DataGridView.Item(0, i).Value
		Me.txtSupplierName.Text = DataGridView.Item(1, i).Value
		Me.txtSupplierSurname.Text = DataGridView.Item(2, i).Value
		Me.txtAddress.Text = DataGridView.Item(3, i).Value
		Me.txtPhoneNumber.Text = DataGridView.Item(4, i).Value
		Me.txtEmailAddress.Text = DataGridView.Item(5, i).Value
	End Sub


my problem is that when i click on an empty row, i got an error.
so how to validate an empty row
please help
Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,178
  • Joined: 02-July 08

Re: Display datagridview selected row in textboxes

Posted 13 August 2009 - 03:04 AM

Databind each textbox's text property to a field in the table.
Was This Post Helpful? 0
  • +
  • -

#6 manutd4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-May 08

Re: Display datagridview selected row in textboxes

Posted 13 August 2009 - 04:01 AM

can u help me out how to databind each textbox
Was This Post Helpful? 0
  • +
  • -

#7 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,178
  • Joined: 02-July 08

Re: Display datagridview selected row in textboxes

Posted 13 August 2009 - 07:32 AM

Select a textbox, in it's property window scroll down to databindings expand it, click on the text dropdown button, your datasource window should open up, then keep expanding the table until you reach the field you want, repeat this for each textbox. Enjoy...
Was This Post Helpful? 1
  • +
  • -

#8 manutd4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-May 08

Re: Display datagridview selected row in textboxes

Posted 13 August 2009 - 10:53 AM

Everything work fine, i just set the datagridview property
Allow user to add new rows to false and all work great
Was This Post Helpful? 0
  • +
  • -

#9 sojab0on  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-June 14

Re: Display datagridview selected row in textboxes

Posted 26 June 2014 - 07:55 AM

i need it in the c# version can someone provide the code as a c# sample

with the following columns
empLastName,

empFirstName,

empExtension,

empEmail,

empOfficeCode,

empReportsTo,

empJobTitle,

empActive,(this isn't a valve it normaly is used with a chckbox )so i know it cant be put in the text box.

code i have so far is this
Cvalve.Text = dataGridView1.CurrentRow.Cells["[empNumber],[empLastName],[empFirstName],[empExtension],[empEmail],[empOfficeCode]"].Value.ToString(); but this line gets a problem it cant find the columns i specified but in hte gridview they are there and even in the database its self that not so strange at all because the gridview is databound to the database using the databinding source for it.

can someone point me out why i get the exception on the columns ?
and maybe point me in the richt direction on how to fix the column not found error.
Was This Post Helpful? 0
  • +
  • -

#10 sojab0on  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-June 14

Re: Display datagridview selected row in textboxes

Posted 27 June 2014 - 04:17 AM

i had changed the code but it give back this exception
The index is out of range. These must not be negative and must be less than the size of the collection.

it is this code now
 private void selectedCellsButton_Click(object sender, EventArgs e)
        {
            Cvalve.Text = dataGridView1.SelectedRows[0].Cells["empNumber"].Value.ToString();
            Cvalve.Text = dataGridView1.SelectedRows[0].Cells["empLastName"].Value.ToString();
            Cvalve.Text = dataGridView1.SelectedRows[0].Cells["empFisrtName"].Value.ToString();
            Cvalve.Text = dataGridView1.SelectedRows[0].Cells["empExtension"].Value.ToString();
            Cvalve.Text = dataGridView1.SelectedRows[0].Cells["empemail"].Value.ToString();
            Cvalve.Text = dataGridView1.SelectedRows[0].Cells["empOfficeCode"].Value.ToString();
            Cvalve.Text = dataGridView1.SelectedRows[0].Cells["empReportsTo"].Value.ToString();
            Cvalve.Text = dataGridView1.SelectedRows[0].Cells["empJobTitle"].Value.ToString();
        }

i cant seen to find a way to get past this exception
the previous code gave back colmn not found while it does exisit in the gridview.
Was This Post Helpful? 0
  • +
  • -

#11 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,178
  • Joined: 02-July 08

Re: Display datagridview selected row in textboxes

Posted 27 June 2014 - 07:37 AM

@sojab0on, not only did you hijack someone else's thread(and an old one at that), but it's C# as well _ wrong forum!
Was This Post Helpful? 0
  • +
  • -

#12 sojab0on  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-June 14

Re: Display datagridview selected row in textboxes

Posted 27 June 2014 - 08:05 AM

i saw it was just that this treat did almost the same as i wanted to do in c#
but i will see to that i dont post in wrong forum again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1