Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

In the code below, I try to see if a certain column of a DataGridView was clicked, and run a report if so.

VB
Private Sub dgv_Report_CellMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgv_Report.CellMouseDoubleClick
        Dim i As Integer = -1
        Dim s As String = String.Empty
        Try
            'Get the drill down report for the column that was double clicked
            If Not e.ColumnIndex < 0 And Not e.RowIndex < 0 And DrillDownColumns.Count > 0 Then
                For Each ddc As DrillDownColumn In DrillDownColumns
                    'Check if one of the trigger columns was clicked
                    s = ddc.TriggerColumn
                    i = CType(sender, DataGridView).Columns(columnName:=s).DisplayIndex
                    If i = e.ColumnIndex Then
                        'Get the parameters
                        For Each p As SqlParameter In ddc.Parameters
                            p.Value = CType(sender, DataGridView).Columns.Item(p.SourceColumn)
                        Next
                        'Run the drill down report
                        RunSubReport(DDC:=ddc)
                    End If
                Next
            End If
        Catch ex As Exception
            EC(ex)
        End Try
    End Sub


The line:
i = CType(sender, DataGridView).Columns(columnName:=s).DisplayIndex
throws an
Quote:
Object reference not set to an instance of an object
error.

When I check in debug, the sender has 9 columns though, including a column by the name that I am testing with.

Can someone please explain this to me, or tell me what it is I am missing?

Regards,
Johan

What I have tried:

MsgBox(CType(sender, DataGridView).Columns.Count.ToString) --> returns a number > 0

I have checked that the column name that I am searching for actually exists.

Otherwise staring myself blind I guess.
Posted
Updated 31-Mar-17 3:50am
Comments
CHill60 31-Mar-17 7:17am    
If that is indeed the line causing the problem then the column name must be wrong - is it exactly the name of the column in the DGV - including any upper case letters, definitely no spaces involved?
Johan Hakkesteegt 31-Mar-17 7:43am    
Doh!

Search string: 2017_2_Sales --> column name: 2017_02_Sales

1 solution

Doh!

Search string: 2017_2_Sales --> column name: 2017_02_Sales
 
Share this answer
 
Comments
CHill60 31-Mar-17 11:20am    
:laugh: Been there - done that too.

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