Solved

vb.net datagridview button

Posted on 2006-10-30
2
1,328 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
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now