[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

get bound datagrid field value for a given row

Posted on 2006-05-05
13
Medium Priority
?
347 Views
Last Modified: 2008-01-09
VB2005
if i populate a listbox using a tableadapter, i can get the value of a field of the selected item like this:  CType(Me.CATEGORIESListBox1.SelectedItem, DataRowView)("IDENT"), and i get the value of the field IDENT in the database for the selected combobox item.

whats the equivalent if i bind a datagrid (and hide the IDENT column from the user) and wanted to get that same field's value from the selected row?

thanks
0
Comment
Question by:newyuppie
  • 5
  • 4
  • 4
13 Comments
 
LVL 10

Expert Comment

by:vadim63
ID: 16614727
Me.DataGridView.CurrentRow.Cells(i).Value
0
 
LVL 13

Author Comment

by:newyuppie
ID: 16614752
how do i get the "i"?
0
 
LVL 7

Accepted Solution

by:
davidrichardson earned 1000 total points
ID: 16614798
Dim s As Integer
s = DataGridView.CurrentRowIndex
textbox1.Text = DataGridView.Item(s, 0)

'0 = the column of the datagrid you want eg 0 = first column 1 = second ect
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 10

Expert Comment

by:vadim63
ID: 16614807
"i" is an index of the columnn.
0
 
LVL 7

Expert Comment

by:davidrichardson
ID: 16614875
or just have a bound textbox then you dont need to hide the column
0
 
LVL 13

Author Comment

by:newyuppie
ID: 16614934
what if i allow the columns to be dynamic, eg the user can reorder columns. can i use davidrichardson's code and replace column index with column name somehow?
0
 
LVL 13

Author Comment

by:newyuppie
ID: 16615032
it doesnt for column index if i HIDE columns from the user... i get error
0
 
LVL 7

Expert Comment

by:davidrichardson
ID: 16615035
no

i would sugest the bound textbox, then it doent matter if the column are moved

textbox1.DataBindings.Add("Text", YOURDATASET, "YOURTABLE.YOURCOLUMNNAME")
0
 
LVL 7

Expert Comment

by:davidrichardson
ID: 16615047
Please ignore ---- no
From last post
0
 
LVL 7

Expert Comment

by:davidrichardson
ID: 16615145
If you get the value from the datagrid you need to set the column width to 0 rather than hiding it.
0
 
LVL 10

Assisted Solution

by:vadim63
vadim63 earned 1000 total points
ID: 16615625
Check this:
By default vb designer assigns the names for datagridview columns like this: Me.DataGridViewTextBoxColumn1.Name = "DataGridViewTextBoxColumn1". You can find it in form1.Designer.vb, eg:

        Me.DataGridViewTextBoxColumn1.DataPropertyName = "IDENT"
        Me.DataGridViewTextBoxColumn1.HeaderText = "IDENT"
        Me.DataGridViewTextBoxColumn1.Name = "DataGridViewTextBoxColumn1"

        Me.DataGridViewTextBoxColumn2.DataPropertyName = "LastName"
        Me.DataGridViewTextBoxColumn2.HeaderText = "LastName"
        Me.DataGridViewTextBoxColumn2.Name = "DataGridViewTextBoxColumn2"   etc

All you need to do is to rename it when your form is loading:

Eg:
        Me.DataGridViewTextBoxColumn1.Name = "IDENT"
        Me.DataGridViewTextBoxColumn2.Name = "LastName"  etc

From now you'll be able to use the real columns names instead of indexes:

Me.DataGridView.CurrentRow.Cells("IDENT").Value
Me.DataGridView.CurrentRow.Cells("LastName").Value      etc

And there's no problem if the column is hidden, or the user would reorder it. It works.
0
 
LVL 10

Expert Comment

by:vadim63
ID: 16615743
BTW, if you complete your datagridview at design time, you can rename your columns at design time too:

Edit columns -> Design -> (Name) -> rename "DataGridViewTextBoxColumn1" to "IDENT"
That's it.
0
 
LVL 13

Author Comment

by:newyuppie
ID: 16627550
thanks guys for replying.
i used all your responses to get a general direction because im new to datagrid. ill give the points to both of you for your help.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question