In my form, I have a combo box and a search button.
When a value is selected from the combo box and search button is clicked.
Values from database is displayed in the DataGridView with a button.
But when search button is clicked again.
Another button gets appended to the datagridview, without clearing the previous button.
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
DataGridView1.Show()
Dim MyDT As New DataTable
Dim MyRow As DataRow
Dim selectCommand As OleDbCommand
Dim colBtn As DataGridViewButtonColumn
colBtn = New DataGridViewButtonColumn
colBtn.Text = "Update"
colBtn.UseColumnTextForButtonValue = True
colBtn.FlatStyle = FlatStyle.Flat
MyDT.Columns.Add(New DataColumn("CustomerID", _
GetType(String)))
MyDT.Columns.Add(New DataColumn("Name", _
GetType(String)))
MyDT.Columns.Add(New DataColumn("NRIC", _
GetType(String)))
MyDT.Columns.Add(New DataColumn("Email", _
GetType(String)))
MyDT.Columns.Add(New DataColumn("Contact No", _
GetType(String)))
Dim ConnString As String = My.Settings.MyBooksConnectionString
Try
Using connection As OleDbConnection = New OleDbConnection(ConnString)
connection.Open()
selectCommand = New OleDbCommand("select customerId,custName,nric,email,contactNo,address,country,code from Customer where (customerId =" & CStr(cboCustId.SelectedValue) & ")", connection)
Dim dr As OleDbDataReader
dr = selectCommand.ExecuteReader()
While dr.Read
MyRow = MyDT.NewRow
MyRow(0) = dr(0)
MyRow(1) = dr(1)
MyRow(2) = dr(2)
MyRow(3) = dr(3)
MyRow(4) = dr(4)
'MyRow(5) = CStr(dr(5)) + " " + CStr(dr(6)) + " " + CStr(dr(7))
MyDT.Rows.Add(MyRow)
DataGridView1.DataSource = MyDT
DataGridView1.AutoGenerateColumns = False
DataGridView1.Columns.Add(colBtn)
End While
connection.Close()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Thanking in Advance