Selecting data in a Gridview

Dohmaker

Member
Joined
Oct 4, 2007
Messages
13
Programming Experience
1-3
Hi everyone,

I've been fighting with that gridview for a while now, so I figured I'd ask some help.

I created a gridview, and this gridview is bound to a database. No problem there. I then added a commandfield (select) to it, so I can select a row. Still not problem there.

Now, where I am stuck its when I want to put information from the selected row and put it in a variable. I just can't figure out how, and I have looked for a while now. I do I do that?
 
Maybe something like this?

VB.NET:
Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
Dim selectedRowCount As Integer = DataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected)
        If selectedRowCount > 0 Then
            For i As Integer = 0 To selectedRowCount - 1
                Dim myVar1 As String = DataGridView1.SelectedRows(i).Cells("fieldnameorindex").Value
                Dim myVar2 As String = DataGridView1.SelectedRows(i).Cells("fieldnameorindex").Value
                ' etc....
            Next
        End If
End Sub
 
yourvariabel = DirectCast(yourdatagridview.CurrentRow.Cells(zzz).Value, tipe)

zzz = index of your column / header column

tipe = string, integer, single, etc
 
hello Icecat,

I tried your option but when it comes to the .currentrow, I get an error saying "CurrentRow is not a member of 'System.Web.UI.WebControl.Gridview'"

have any idea what this means, or how to correct that?

also, in both what Icecat and Mentalhard suggested, when it comes to the .value, I get an error saying: "'Value' is not a member of 'System.Web.UI.Webcontrols.Tablecell' "

so yeah, without the .value its hard to get a cell's value :(

Have any suggestions?
 
Thread moved to Web Grids forum.
 
Back
Top