Solved

vb.net datagridview button

Posted on 2006-10-30
2
1,363 Views
Last Modified: 2008-01-09
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
Comment
Question by:jackjohnson44
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 39

Accepted Solution

by:
appari earned 500 total points
ID: 17839921
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
 
LVL 6

Expert Comment

by:manch
ID: 17839922

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question