Solved

vb.net datagridview button

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 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