I'm trying to add a CheckBox Column to RadGridView control using code.

Below is my code:

Sub AddCheckColumnGrid()

    '  Dim chkSelection As New DataGridViewCheckBoxColumn // Microsoft Control
    Dim chkSelection As New GridViewCheckBoxColumn '// Telerik Control
    With chkSelection
        .HeaderText = "Check"
        .Name = "Check"
        .Width = 80
    End With

    gvDisplay.Columns.Insert(0, chkSelection)
End Sub

The form with the GridView is displayed as a DialogBox using the code below

.ShowDialog()

My Challenge: When I open the form for the first time, it works fine. When I try opening it again, I get the error message

A Column with the same Name exists in the collection.

What I have done so far:

1) I tried the code with the

Visual Studio Default DataGridView and DataGridViewCheckBoxColumn. Error Image

The Microsoft DataGridView doesn't give error but anytime I open the form, a new Check Column is added to the grid. If I open it 10 times, 10 check columns will be added. That is not what I want. No matter how many times I open it, I want just one column added to the grid. Image

2) I added:

gvDisplay.Columns.Clear()

I got that suggestion from Refreshing RadGridView with Programtically added Button : A column with the same Name already exists in the collection

Unfortunately, I didn't make me happy :( When I open the form, only the "Check" Column shows. All the Columns from the Database don't. Check Display here

3) I'v read few links to the Telerik Documentation but I'm still not getting it.

How do I fix the error message?:

A Column with the same Name exists in the collection.

1

There are 1 best solutions below

0
On

The code below did the trick for me.

I put the Insertion in IF Condition

If NOT gvDisplay.Columns.Contains("check") Then
gvDisplay.Columns.Insert(0, chkSelection)
End If