why click button manny times,each time add column on data gridview???

kank

Active member
Joined
Dec 12, 2011
Messages
26
Programming Experience
Beginner
My code is below, I feel confused that why each time I click on this code button, it adds one column as the the result on below pic.

VB.NET:
Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click

        'Refresh bsource and databinding
        DataGridView1.DataSource = Nothing
        bsource.RemoveFilter()

        sqlconn.Open()
        'da = New SqlDataAdapter(sql, sqlconn)
        'commandBuilder = New SqlCommandBuilder(da)
        ds = New DataSet()
        da.Fill(ds, "New")
        bsource.DataSource = ds.Tables("New")

        Me.DataGridView1.DataSource = bsource
        Me.DataGridView1.Columns(0).Visible = False

        Dim btn As New DataGridViewButtonColumn()
        DataGridView1.Columns.Add(btn)
        btn.HeaderText = "Rate Update"
        btn.Text = "Click Here"
        btn.Name = "btn"
        btn.UseColumnTextForButtonValue = True
        sqlconn.Close()
    End Sub
result.png
pls kindly help. Thanks alot
 
I'm confused as to how you can be confused. Every time you click the Button you are executing this code:
VB.NET:
DataGridView1.Columns.Add(btn)
Why would you NOT expect a column to be added every time?
 
I didn't say that. You should be telling us whether the Button needs to be clicked more than once, not the other way around. It's your app so it should be designed how you want it to work. If the Button needs to be clicked more than once then it should be clickable more than once.

What I said was that that code is going to be executed every time you click the Button so, if you are going to click the Button more than once, you're going to execute the code more than once and you're going to add multiple columns. If you need to click the Button multiple times but you only want to add one column then the code to add the column should not be in the Click event handler of the Button, or if it is then it should be inside a conditional block so it will only be executed the first time.
 
Back
Top