4 Replies - 17321 Views - Last Post: 07 August 2008 - 08:20 AM Rate Topic: -----

#1 MrWobbles  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 31
  • View blog
  • Posts: 328
  • Joined: 11-April 08

Getting the Value from a datagridview

Post icon  Posted 07 August 2008 - 07:04 AM

I had a question earlier that asked how to get the value from the datagridview cell that I wanted and for the most part I think I have it figured out. I am fairly certain I am going to the right place, but I am getting weird values for what would be in the cells - values that I do not have in them.

For Small Text Fields i get: "text"
Large Text Fields i get: "lrgText"
Integer: "5"
Double: "6"
Date: "1/1/1900"
Boolean (tinyInt): "1"

I have checked around and it seems that I am doing this correctly, but I can't seem to get the values I want, any suggestions are appreciated. I reposted the code that I am using below.

	Private Sub fillTextBoxes()
		Dim columnX As DataGridViewColumn
		Dim rowX As Integer
		Dim cVal As String = ""
		Dim i As Integer = 0

		rowX = dgView.CurrentRow.Index

		While i < Me.portTabs.SelectedTab.Controls.Item(0).Controls.Item(0).Controls.Count
			For Each columnX In dgView.Columns


				If Me.portTabs.SelectedTab.Controls.Item(0).Controls.Item(0).Controls.Item(i).Name = columnX.Name Then
					cVal = dgView.Item(columnX.Name, rowX).Value
					'rowX.Cells(columnX.Name.ToString).Value()
					MsgBox(cVal)
					Me.portTabs.SelectedTab.Controls.Item(0).Controls.Item(0).Controls.Item(i).Controls.Item(0).Text = cVal
					Exit For
				End If

			Next
			i = i + 1
		End While
	End Sub



Is This A Good Question/Topic? 0
  • +

#3 Bort  Icon User is offline

  • Ill-informed Mongoloid
  • member icon

Reputation: 409
  • View blog
  • Posts: 3,001
  • Joined: 18-September 06

Re: Getting the Value from a datagridview

Posted 07 August 2008 - 07:10 AM

For this line:

cVal = dgView.Item(columnX.Name, rowX).Value

try this:

cVal = dgView.Item(columnX.Name, rowX).Value.ToString

Also, you may want to see if you can use the column index instead of the name. Remember that just about all indexes in VB and 0-based.
Was This Post Helpful? 1

#4 MrWobbles  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 31
  • View blog
  • Posts: 328
  • Joined: 11-April 08

Re: Getting the Value from a datagridview

Posted 07 August 2008 - 07:35 AM

I've tried both, using the index and adding the ToString, this still returns the same values, even if the cell is empty. I don't even know where it is getting the values from, as they are nowhere else in my code like that. Thanks for the suggestions though, I will continue looking.
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2264
  • View blog
  • Posts: 9,470
  • Joined: 29-May 08

Re: Getting the Value from a datagridview

Posted 07 August 2008 - 08:14 AM

{Form}.{Name of DataGridView}({Column Index}, {Row Index}).Value = "Hello" 'Sets the cell to Hello
ReadValue={Form}.{Name of DataGridView}({Column Index}, {Row Index}).Value ' Reads back to value


Was This Post Helpful? 0
  • +
  • -

#6 MrWobbles  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 31
  • View blog
  • Posts: 328
  • Joined: 11-April 08

Re: Getting the Value from a datagridview

Posted 07 August 2008 - 08:20 AM

I realized what I was doing, see I have multiple datagridviews in the form, one for each table actually, and I was using the wrong one, I fixed it by declaring a new datagridview object in the function and then setting it equal to the datagridview I would be using, this way it stays semi-dynamic, and I am using the same one.. like this:

Dim dataGridV As DataGridView = Me.portTabs.SelectedTab.Controls.Item(0).Controls.Item(1).Controls.Item(0)



I still don't know where it got those values btw

This post has been edited by MrWobbles: 07 August 2008 - 08:32 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1