Copying values from DataGridView to a string variable

JackiTyan

Member
Joined
Oct 4, 2006
Messages
6
Programming Experience
Beginner
Hi, i've read about copying cell contents from DataGridView to Textbox in VB.NET Help, but i think it's incomplete.
What i need to do is for the DataGridView's SelectionMode to be FullRowSelect, but i only want to assign a certain column's value to my search string, so i can specifically set my SQL statement to populate a series of textboxes that contains data returned from said SQL statement.

VB.NET:
Clipboard.SetDataObject(DataGridView.GetClipboardContent())
Dim SelRow As String = Clipboard.GetText()
Preceding lines of code gets the value of the entire selected row, but i only need the leftmost value, before the space that separates the 1st column from the 2nd.
I have tried looking for the 1st occurrence of " " with InStr() function, but it seems that the " " between the returned columns is not whitespace (" ") but something else. I think it's a TAB, but i dont know how to test for TAB or other special characters using InStr() function.

I hope somebody can help me with this. Thanks in advance.
 
Last edited by a moderator:
By index or column name:
VB.NET:
[SIZE=2]DataGridView1.SelectedRows(0).Cells([/SIZE][SIZE=2][COLOR=#a31515]"Column1"[/COLOR][/SIZE][SIZE=2]).Value[/SIZE]
[SIZE=2]DataGridView1.SelectedRows(0).Cells([/SIZE][SIZE=2][COLOR=#a31515]0[/COLOR][/SIZE][SIZE=2]).Value[/SIZE]
Note, you should not modify the clipboard without users will without preserving and restoring the content.

Also, Instr function is no longer used in VB.Net, instead use the strings own .IndexOf function method. The string constant for Tab key is still 'vbTab'.
 
Back
Top