Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1376
  • Last Modified:

vb.net datagridview button

I have a datagridview where I insert a button.

I call the same function many times which gets a dataset from a db, then sets it to the grid.
After that I add the button with the code below.

Each time I call the function, a new button is created, how do I only do it if it does not exist?

        Dim buttons As New DataGridViewButtonColumn()
        With buttons
            .HeaderText = "Delete"
            .Text = "Delete"
            .UseColumnTextForButtonValue = True
            .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
            .FlatStyle = FlatStyle.Standard
            .DisplayIndex = 0
        End With
0
jackjohnson44
Asked:
jackjohnson44
1 Solution
 
appariCommented:
try like this

        if not yourGridName.columns.contains("Delete") then      
        Dim buttons As New DataGridViewButtonColumn()
        With buttons
            .HeaderText = "Delete"
            .Text = "Delete"
            .name ="Delete"
            .UseColumnTextForButtonValue = True
            .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
            .FlatStyle = FlatStyle.Standard
            .DisplayIndex = 0
        End With
        End if

0
 
manchCommented:

U have to add this condition before adding the column to the Grid

the Logic is as follows :
(let DataGridView1 )

        'Here is ur code for creating a New Button Column
        Dim buttons As New DataGridViewButtonColumn()
        With buttons
            .HeaderText = "Delete"
            .Text = "Delete"
            .UseColumnTextForButtonValue = True
            .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
            .FlatStyle = FlatStyle.Standard
            .DisplayIndex = 0
        End With

        'Here is the code for checking the existance of the column in the grid view
        Dim pbFound As Boolean = False
        Dim piCnt As Int16 = 0

        While (Not (pbFound) And (piCnt <= (DataGridView1.Columns.Count - 1)))

            If (DataGridView1.Columns(piCnt).HeaderText = "Delete") Then
                pbFound = True
            End If
            piCnt = piCnt + 1
        End While
        'If column does not exists in the datagridview1 then adding it to our datagridview1
        If (pbFound = false ) Then
            DataGridView1.Columns.Add(buttons)
        End If


Try It
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now