Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please I need your help on this issue:

In VB.NET, I have a form with a panel. This panel is populated with multiple datagridview controls with code pragmatically at run time. Each datagridview gets data from my MS SQL database.----I have been able to achieve this successfully. My problem is how to get the cell value of any of the datagridview on a DoubleClick event.

I have spent three days trying to figure this out and searched the internet but to no avail. Any help will be appreciated.

What I have tried:

VB.NET
'''My Load Event Code which creates all database controls , fetches data from the datagridview and arranges them in the panel:


 Dim Dayz As Int32 = 30

        For i As Int32 = 1 To Dayz
            Dim datagridview1 As New DataGridView()
            datagridview1.Name = i.ToString()

            AddHandler datagridview1.CellDoubleClick, AddressOf CellDoubleClick

            Dim col As New DataGridViewTextBoxColumn
            col.HeaderText = i.ToString()
            datagridview1.Columns.Add(col)
            datagridview1.AutoGenerateColumns = False

            'gets data from SQL
            SQL.RunQuery("SELECT Subject FROM Appointments WHERE Day = '" & i.ToString() & "'AND Year = '" & textBoxYear.Text & "'AND Month = '" & lbl_Month.Text & "' AND Owner = '" & MainInterface.cbo_Users.Text & "'")
            If SQL.SQLDataset.Tables.Count > 0 Then
                For Each R As DataRow In SQL.SQLDataset.Tables(0).Rows
                    datagridview1.DataSource = SQL.SQLDataset.Tables(0)
                    For k As Integer = 0 To datagridview1.Columns.Count - 1
                        datagridview1.Columns(k).DataPropertyName = "Subject"
                    Next
                Next
            End If

            'arranges all datagridview in the panel
            datagridview1.SetBounds(x, y, 151, 107)
            x += 153
            If ndayz = 7 Then
                x = 0
                ndayz = 0
                y += 109
            End If
            Panel1.Controls.Add(datagridview1)
        Next
'' I do not have any issue with the above code. However in the doubleclick event, I get an Index out of range exception:

 Private Sub CellDoubleClick(sender As System.Object, e As DataGridViewCellEventArgs)

           Msgbox (datagridview1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
'''' I get an index out of range exception on this line.

 End Sub
Posted
Updated 7-Jan-17 8:44am
v2
Comments
Maciej Los 27-Dec-16 3:41am    
Your code is SQL Injection vulnerable!
Richard MacCutchan 27-Dec-16 3:42am    
Which index is out of range and why?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900