"Object reference not set to an instance of an object" Error when i want to Show the Columns in Datagridview

Viewed 25

I have dynamically created two columns SourceProperty and DescriptionProperty when i want to show those columns in datagridview with Button Click event it is showing "Object reference not set to an instance of an object."

Note:When i click the button First it is not showing Error Worked Perfectly But when i click on the Button Second Time i got An "Object reference exception" at the below visible Property Code.

 DataGridView1.Columns("DescriptionProperty").Visible = True

 DataGridView1.Columns("SourceProperty").Visible = True

Below Code represents How i created the columns and bind to datagridview

vb.net

 Dim AytC As Keyoti.RapidSpell.Grid.AYTDataGridViewTextBoxColumn = New Keyoti.RapidSpell.Grid.AYTDataGridViewTextBoxColumn()
            AytC.HeaderText = "Description"
            AytC.Name = "DescriptionProperty"
 gvInsuredData.Columns.Add(AytC)

            Dim AytCtb As Keyoti.RapidSpell.Grid.AYTDataGridViewTextBoxColumn = New Keyoti.RapidSpell.Grid.AYTDataGridViewTextBoxColumn()
            AytCtb.HeaderText = "Source"
            AytCtb.Name = "SourceProperty"
 gvInsuredData.Columns.Add(AytCtb)

            If objcnn.State = ConnectionState.Closed Then objcnn.Open()
            
            gvInsuredData.Columns("DescriptionProperty").Visible = True
            gvInsuredData.Columns("SourceProperty").Visible = True
           
            cmd = New SqlCommand("select *from tblproperty where ClaimID = " & ClaimID & " order by ItemID,PageNo", objcnn)
            
            Dim sda As New SqlDataAdapter(cmd)
            Dim ds As New DataSet()
            sda.Fill(ds)
            gvInsuredData.DataSource = ds.Tables(0)
            gvInsuredData.Columns("DescriptionProperty").DataPropertyName = "personalPropertyDescription"
            gvInsuredData.Columns("SourceProperty").DataPropertyName = "personalPropertySource"
            objcnn.Close()

0 Answers
Related