简体   繁体   中英

DataGridView is not displaying my delete button

I have a problem in my 2nd load of data using a DataGridView.

Here is my DataGridView and has a delete button when the data is first loaded:

在此处输入图片说明

On my 2nd load of data, this is the error I get:

在此处输入图片说明

After I click Ok , this is what happens in my DataGridView:

在此处输入图片说明

The delete button is gone.

This is my code:

Private Sub loaddata()
    Dim con As New MySqlConnection
    Dim cmd As New MySqlCommand
    con.ConnectionString = "server=192.168.1.10;database=orderingsystem;username=server;password=server"
    Dim da As New MySqlDataAdapter
    Dim ds As New DataTable
    Dim source As New BindingSource
    Try
        con.Open()
        Dim query As String
        Dim total As Double
        Dim btn As New DataGridViewButtonColumn
        btn.HeaderText = "Action"
        btn.Text = "Delete"
        btn.Name = "btn"
        btn.UseColumnTextForButtonValue = True

        query = "SELECT prodID, SUM(prodQty) as QTY, prodname as Name, prodPrice*SUM(prodQty) as Total from orderedlist where table_name = '" & Form1.Table_1.Name & "' GROUP BY prodID"
        cmd = New MySqlCommand(query, con)
        da.SelectCommand = cmd
        da.Fill(ds)
        source.DataSource = ds
        dgv_myOrder.DataSource = ds
        dgv_myOrder.Columns(0).Visible = False

        For i As Integer = 0 To dgv_myOrder.Rows.Count - 1
            total += Convert.ToDecimal(dgv_myOrder.Rows(i).Cells(3).Value).ToString("0.00")

        Next
        dgv_myOrder.Columns.Add(btn)

        lbl_Total.Text = Decimal.Parse(total).ToString("0.00")
        con.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        con.Dispose()
    End Try
End Sub

Private Sub dgv_myOrder_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgv_myOrder.CellClick

    If e.RowIndex = dgv_myOrder.NewRowIndex Or e.RowIndex < 0 Then
    ElseIf e.ColumnIndex = dgv_myOrder.Columns("btn").Index Then

        Dim connection As String = "server=192.168.1.10;database=orderingsystem;username=server;password=server;"
        Dim con2 As New MySqlConnection(connection)
        con2.Open()

        Dim data As String = dgv_myOrder.SelectedRows(0).Cells(0).Value.ToString
        Label2.Text = data

        Dim rd2 As MySqlDataReader
        Dim cmd2 As New MySqlCommand("UPDATE orderedlist SET cancelstatus = 1 WHERE prodID = '" & Label2.Text & "' and table_name = '" & Form1.Table_1.Name & "'", con2)

        rd2 = cmd2.ExecuteReader
        con2.Close()
        con2.Open()

        Dim rd3 As MySqlDataReader
        Dim cmd3 As New MySqlCommand("UPDATE tableassistance SET cancellation = 1 WHERE table_name = '" & Form1.Table_1.Name & "'", con2)

        rd3 = cmd3.ExecuteReader

        MessageBox.Show("Wait for confirmation!", "System")
        Me.Close()
        Label2.Text = ""
    End If
End Sub

I call loaddata() in my form load.

First remove this Line:

  dgv_myOrder.Columns.Add(btn)

Don't add the button to datagridview inside code create it form the designer and use binding for other columns.

Steps :

1 . Create all your datagrdiview columns , using add columns button in the top right of the datagridview

this is how you add columns

2 .after you add the columns , bind every column to its related Column in database like this , you can get this window when you click on EditColumns in datagidview designer , change the DataPropertyName in the properties of each column change it to the name of the datatable column name that you want it to preview but not add it to the delete column button:

Binding the columns

3 . go to the properties of your delete button column in the datagridview you will find property called DefalutCellStyle inside it will find NullValue change it to "Delete".

Change the null value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM