Hi! I am using Data Grid View to display the data from my database. This code works properly. It displays the data.
This code is inserted in the Form_Load of the form.
Dim cmdCount, cmdGet As New iDB2Command
Dim daGet As New iDB2DataAdapter
Dim dsGet As New DataSet
Dim sqlCount, sqlGet As String
Dim x, i As Integer
conn.ConnectionString = str
conn.Open()
tsSearch.Visible = False
tsDates.Visible = False
'****Display the total record of all the items in exptranslog
sqlCount = "SELECT COUNT(*) AS count FROM exptranslog"
cmdCount.CommandText = sqlCount
cmdCount.Connection = conn
tsTotal.ForeColor = Color.Blue
tsTotal.Text = Convert.ToInt32(cmdCount.ExecuteScalar())
'****Display all the records in dgTranslog
sqlGet = "SELECT * FROM exptranslog ORDER BY recno DESC"
cmdGet.CommandText = sqlGet
cmdGet.Connection = conn
daGet.SelectCommand = cmdGet
daGet.Fill(dsGet, tblTranslog)
dgTranslog.DataSource = dsGet
dgTranslog.DataMember = tblTranslog
With dgTranslog
.RowHeadersVisible = False
.Columns(0).HeaderCell.Value = "No"
.Columns(1).HeaderCell.Value = "Date Performed"
.Columns(2).HeaderCell.Value = "Time Performed"
.Columns(3).HeaderCell.Value = "Performed By"
.Columns(4).HeaderCell.Value = "Action Taken"
.Columns(0).Width = 40
.Columns(1).Width = 110
.Columns(2).Width = 110
.Columns(3).Width = 90
.Columns(4).Width = 340
For i = 0 To 3
.Columns(i).SortMode = False
.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
Next
.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.ColumnHeadersDefaultCellStyle.Font = New Font(dgTranslog.Font, FontStyle.Bold)
.Columns(1).DefaultCellStyle.Format = "MMM d, yyyy"
.Columns(2).DefaultCellStyle.Format = "hh:mm:ss tt"
'****Own counter not from the database itself
For x = 0 To tsTotal.Text - 1
.Rows(x).Cells(0).Value = x + 1
Next
End With
conn.Close()
Now, my problem is that I will also be using the same data grid view for listing the results of the search when a button is clicked. I have two date pickers to get the date from and date to. The user will filter the date range for his search. Data Grid View will then display the results.
Now, I don't know how could I reuse the same dgTranslog
- data grid view again to display the results. I tried to copy paste the same line of codes to button_click
but error occurs. See pix below.