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

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

.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
End With


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.


2 Years
Discussion Span
Last Post by Michael_9

Try this:

Public Function filterDataSet(ByVal ds As DataSet) As DataView

    Dim dv As DataView = New DataView()

    dv = ds.Tables(0).DefaultView
    dv.RowFilter = "startDate > #1/30/2008# and endDate < #1/30/2010#"

    Return dv
End Function


dgTranslog.DataSource  = Nothing
dgTranslog.DataSource = filterDataSet(dsGet)

Could replace:

dv = ds.Tables(0).DefaultView


dv = ds.Tables(tblTranslog).DefaultView

The following may need to be changed:

"startDate > #1/30/2008# and endDate < #1/30/2010#"

and the format of the date may depend on your computer locale settings.

The above is untested.

Code adapted from:

DataView RowFilter with multiple conditions

DataView RowFilter Syntax

Edited by cgeier


You can also try something like the following:

dgTranslog = New DataGridView()

Dim tbColDatePerformed As DataGridViewTextBoxColumn = New DataGridViewTextBoxColumn()

tbColDatePerformed.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
tbColDatePerformed.DataPropertyName = Nothing
tbColDatePerformed.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
tbColDatePerformed.HeaderText = "Date Performed"
tbColDatePerformed.HeaderCell.Value = "Date Performed"
tbColDatePerformed.Name = "tbColDatePerformed"
tbColDatePerformed.ReadOnly = False
tbColDatePerformed.Resizable = DataGridViewTriState.True
tbColDatePerformed.SortMode = DataGridViewColumnSortMode.NotSortable
tbColDatePerformed.Width = 110


Repeat the above for each column.


''worth a shot

Edited by Michael_9: More info

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.